/*****************************************
Impact of Social Networks on EITC Claiming Behavior

Review of Economics and Statistics

Prepared by: Riley Wilson

******************************************/
/*First, you must set the directories to the file where 
(1) the data is located
(2) the output should be placed
(3) the proprietary SCI data is located*/

clear
set more off
global data "C:\Users\rwilson9\Box\Research\eitc_networks\submission\restat\final_submission\data"
global output "C:\Users\rwilson9\Box\Research\eitc_networks\submission\restat\final_submission\output"
global fbdata "C:\Users\rwilson9\Box\Research\FB_socialnetwork_data"

global controls "popshare_female popshare_nhblack popshare_nhother popshare_hisp" 
global acscontrols "age_18_34 age_35_54 age_55_64 age_over64 div_sepwidow nevmar blacknh asiannh othernh hisp"

/*******************************
*********FIGURES****************
*******************************/
/******************************
Figure 1. Share of States with Own State EITC Supplement Over Time
******************************/
cd $data
use state_eitcrates1985_2018, clear

gen anyeitc = eitc_pct>0

collapse (sum) anyeitc, by(year)

drop if year == 2018

cd $output
twoway (scatter anyeitc year if year<=2013, connect(line) mcolor(black) lcolor(black)) ///
	, xtitle(Year) xlabel(1985(5)2010) ytitle(Number of States with Own EITC)  ///
	graphregion(color(white)) 
	
graph export states_weitc_bw.png, replace

/***********************
Figure 2. Maps of Out-of-State Friends Exposed to State EITC
***********************/
cd $data
use cty_eitcnetwork1999_2013, clear

reghdfe eitc_rate eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
sum eitcexp_fblinkspp [aw = pop2000] if  e(sample) == 1, de
gen std_eitcexp_fblinkspp = (eitcexp_fblinkspp-r(mean))/r(sd)

gen t = std_eitcexp_fblinkspp if year == 2000
bys ctyfips: egen std_eitcexp_fblinkspp2000 = max(t)
drop t

gen t = std_eitcexp_fblinkspp if year == 2013
bys ctyfips: egen std_eitcexp_fblinkspp2013 = max(t)
drop t

gen d_std_eitcexp_fblinkspp = std_eitcexp_fblinkspp2013-std_eitcexp_fblinkspp2000
gen pct_std_eitcexp_fblinkspp = (d_std_eitcexp_fblinkspp/std_eitcexp_fblinkspp2000)*100
//keep if inlist(year,2000,2013)
keep ctyfips year pctany_steitc steitc_pct std_eitcexp_fblinkspp* d_std_eitcexp_fblinkspp pct_std_eitcexp_fblinkspp
//reshape wide pctany_steitc steitc_pct eitcexp_fblinkspp, i(ctyfips) j(year)

tempfile cty_rates
save `cty_rates'

cd $data
cd shapefiles

use countydb, clear
destring GEOID, gen(ctyfips)
merge 1:m ctyfips using `cty_rates'

cd $data
cd shapefiles
spmap d_std_eitcexp_fblinkspp using countycoord if year == 2000 & ~inlist(STATEFP,"02","15","60","66","69","72","78"), id(id) ///
	clmethod(custom) clbreak(0 .5 1 1.5 2 20) fcolor(navy*.25 navy*.5 navy*.75 navy navy*1.25) ocolor( navy*.75 navy navy*1.25 navy*1.5 navy*1.75) legend( pos(4) title("Standard Deviation", size(small)) size(*1.2)  subtitle("{&Delta} Number of Friends (p.p.)", size(small)) label(2 "Less than 0.5 S.D.") label(3 "0.5-1 S.D.") label(4 "1-1.5 S.D.") label(5 "1.5-2 S.D.") label(6 "Over 2 S.D."))  saving(eitcfblinks_mapchange, replace)
cd $output
graph export eitcfblinks_mapchange.png, replace

cd $data
cd shapefiles
spmap d_std_eitcexp_fblinkspp using countycoord if year == 2000 & ~inlist(STATEFP,"02","15","60","66","69","72","78"), id(id) ///
	clmethod(custom) clbreak(0 .5 1 1.5 2 20) fcolor(black*.2 black*.4 black*.6 black*.8 black*1) ocolor( black*.75 black black*1.25 black*1.5 black*1.75) legend( pos(4) title("Standard Deviation", size(small)) size(*1.2)  subtitle("{&Delta} Number of Friends (p.p.)", size(small)) label(2 "Less than 0.5 S.D.") label(3 "0.5-1 S.D.") label(4 "1-1.5 S.D.") label(5 "1.5-2 S.D.") label(6 "Over 2 S.D."))
cd $output
graph export eitcfblinks_mapchange_bw.png, replace


/******************************
Figure 3. Direct Impact on Earnings Distribution
******************************/
cd $data
use cty_eitcnetwork1999_2013, clear
foreach i in 0 5 10 15 20 25 30 35 40 {
	gen pcteitc_`i' = (eagi`i'/eitc_returns)*100
}
egen rowtotal = rowtotal(pcteitc_*)

foreach outcome of varlist pcteitc* {
	gen ln`outcome' = ln(`outcome')
}
gen haveeitc = eitc_pct >0
gen pct_eitcrange = ((tagi0+tagi5+tagi10+tagi15+tagi20+tagi25+tagi30+tagi35)/tot_returns)

gen t = eitc_rate if year == 1999
bys ctyfips: egen eitc_rate1999 = max(t)

drop t
gen haveeitc_pctrange = haveeitc*eitc_rate1999

gen cutoff = _n*5-5 if _n<=9
gen beta = .
gen se = .
gen init_v = .
gen pre_lb = .
gen pre_ub = .
gen m_lb = .
gen m_ub = .

reghdfe ave_eitc haveeitc_pctrange $controls [aw = pop2000] if pop2000 ~= ., absorb(ctyfips st_yr) vce(cluster stfips)
	gen samp = e(sample) == 1
//Effect on Levels
foreach outcome in 0 5 10 15 20 25 30 35 40 {
	reghdfe pcteitc_`outcome' haveeitc_pctrange  $controls [aw = pop2000] if pop2000 ~= ., absorb(ctyfips st_yr) vce(cluster stfips)
	replace beta = _b[haveeitc_pctrange] if cutoff == `outcome'
	replace se = _se[haveeitc_pctrange] if cutoff == `outcome'
	scalar edf_r`outcome' = e(df_r)
	qui sum pcteitc_`outcome' [aw = pop2000] if e(sample) == 1
	replace init_v = r(mean) if cutoff == `outcome'
}

egen t = sum(init_v)

gen pre_val = init_v+beta

foreach outcome in 0 5 10 15 20 25 30 35 40 {

	replace m_lb = beta-se*invttail(`=scalar(edf_r`outcome')',.025) if cutoff == `outcome'
	replace m_ub = beta+se*invttail(`=scalar(edf_r`outcome')',.025) if cutoff == `outcome'
	
	replace pre_lb = pre_val-se*invttail(`=scalar(edf_r`outcome')',.025) if cutoff == `outcome'
	replace pre_ub = pre_val+se*invttail(`=scalar(edf_r`outcome')',.025) if cutoff == `outcome'
}

twoway (bar pre_val cutoff, barwidth(3) lcolor(navy*.4) fcolor(navy*.4)) ///
	(bar init_v cutoff, barwidth(5) lcolor(black) fcolor(none)) ///
	(rcap pre_ub pre_lb cutoff, lcolor(black) lwidth(vthin)) ///
	, legend(order(1 "After Mean Impact" 2 "Pre-EITC") rows(1)) graphregion(color(white)) ///
	xlabel(0(5)40)
	
//I want the cutoff to be at the lower end of the bar and in thousands of dollars
gen cutoff_mod = cutoff*1000+2500
lab def cutoffs 2500 "Under $5k" 7500 "$5-10K" 12500 "$10-15K" 17500 "$15-20K" 22500 "$20-25K" 27500 "$25-30K" 32500 "$30-35K" 37500 "$35-40K" 42500 "$40-50K", replace
lab val cutoff_mod cutoffs


//I also want to plot the EITC schedule
gen eitc_cutoff2 = .
replace eitc_cutoff2 = 0 if _n == 1
replace eitc_cutoff2 = 12590 if _n == 2
replace eitc_cutoff2 = 16450 if _n == 3
replace eitc_cutoff2 = 40363 if _n == 4

gen eitc_amnt2 = .
replace eitc_amnt2 = 0 if _n == 1
replace eitc_amnt2 = 5036 if _n == 2
replace eitc_amnt2 = 5036 if _n == 3
replace eitc_amnt2 = 0 if _n == 4

gen eitc_cutoff3 = .
replace eitc_cutoff3 = 0 if _n == 1
replace eitc_cutoff3 = 12590 if _n == 2
replace eitc_cutoff3 = 16450 if _n == 3
replace eitc_cutoff3 = 43352 if _n == 4

gen eitc_amnt3 = .
replace eitc_amnt3 = 0 if _n == 1
replace eitc_amnt3 = 5666 if _n == 2
replace eitc_amnt3 = 5666 if _n == 3
replace eitc_amnt3 = 0 if _n == 4

lab val eitc_cutoff2 cutoffs
lab val eitc_cutoff3 cutoffs

twoway (bar beta cutoff_mod, barwidth(5000) lcolor(black) fcolor(none) yaxis(1)) ///
	(rcap m_ub m_lb cutoff_mod, lcolor(black) lwidth(vthin) yaxis(1)) ///
	(line eitc_amnt3 eitc_cutoff3, lwidth(thin) lpattern(__) lcolor(navy*.6) yaxis(2) ///
	text(.08 36000  "3+ Eligible Children", size(vsmall))) ///
	(pcarrowi .076 36000 .059 32500 , color(black)) ///
	(line eitc_amnt2 eitc_cutoff2, lwidth(thin) lpattern(_-_) lcolor(maroon*.6) yaxis(2) ///
	text(.048 42000  "2 Eligible Children", size(vsmall))) ///
	(pcarrowi .044 42000 .031 35000 , color(black)) ///
	, legend(off /*order(1 "") rows(1)*/) graphregion(color(white)) ///
	xlabel(2500(5000)42500, valuelabels labsize(small)) xtitle("") ytitle(Percentage Points, axis(1)) ///
	yscale(range(-6500 6400) axis(2)) ylabel(0(2000)6000, axis(2)) ylabel(-.15(.05).15, axis(1)) ytitle("EITC Amount ($)", axis(2))
	
	
cd $output
graph export impact_earningdist_direct_instate.png, replace

twoway (bar beta cutoff_mod, barwidth(5000) lcolor(black) fcolor(none) yaxis(1)) ///
	(rcap m_ub m_lb cutoff_mod, lcolor(black) lwidth(vthin) yaxis(1)) ///
	(line eitc_amnt3 eitc_cutoff3, lwidth(thin) lpattern(__) lcolor(black*.6) yaxis(2) ///
	text(.08 36000  "3+ Eligible Children", size(vsmall))) ///
	(pcarrowi .076 36000 .059 32500 , color(black)) ///
	(line eitc_amnt2 eitc_cutoff2, lwidth(thin) lpattern(_-_) lcolor(black*.4) yaxis(2) ///
	text(.048 42000  "2 Eligible Children", size(vsmall))) ///
	(pcarrowi .044 42000 .031 35000 , color(black)) ///
	, legend(off /*order(1 "") rows(1)*/) graphregion(color(white)) ///
	xlabel(2500(5000)42500, valuelabels labsize(small)) xtitle("") ytitle(Percentage Points, axis(1)) ///
	yscale(range(-6500 6400) axis(2)) ylabel(0(2000)6000, axis(2)) ylabel(-.15(.05).15, axis(1)) ytitle("EITC Amount ($)", axis(2))

cd $output
graph export impact_earningdist_direct_instate_bw.png, replace
/******************************
Figure 4. Impact on Earnings Distribution
******************************/
cd $data
use cty_eitcnetwork1999_2013, clear
foreach i in 0 5 10 15 20 25 30 35 40 {
	gen pcteitc_`i' = (eagi`i'/eitc_returns)*100
}
reghdfe eitc_rate eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= ., absorb(ctyfips st_yr) vce(cluster stfips)
sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1, de
gen std_eitcexp_fblinkspp = (eitcexp_fblinkspp-r(mean))/r(sd)

egen rowtotal = rowtotal(pcteitc_*)

foreach outcome of varlist pcteitc* {
	gen ln`outcome' = ln(`outcome')
}

gen cutoff = _n*5-5 if _n<=9
gen beta = .
gen se = .
gen init_v = .
gen pre_lb = .
gen pre_ub = .
gen m_lb = .
gen m_ub = .

reghdfe ave_eitc std_eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= ., absorb(ctyfips st_yr) vce(cluster stfips)
	gen samp = e(sample) == 1
//Effect on Levels
foreach outcome in 0 5 10 15 20 25 30 35 40 {
	reghdfe pcteitc_`outcome' std_eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= ., absorb(ctyfips st_yr) vce(cluster stfips)
	replace beta = _b[std_eitcexp_fblinkspp] if cutoff == `outcome'
	replace se = _se[std_eitcexp_fblinkspp] if cutoff == `outcome'
	scalar edf_r`outcome' = e(df_r)
	qui sum pcteitc_`outcome' [aw = pop2000] if e(sample) == 1
	replace init_v = r(mean) if cutoff == `outcome'
}

egen t = sum(init_v)

gen pre_val = init_v+beta

foreach outcome in 0 5 10 15 20 25 30 35 40 {

	replace m_lb = beta-se*invttail(`=scalar(edf_r`outcome')',.025) if cutoff == `outcome'
	replace m_ub = beta+se*invttail(`=scalar(edf_r`outcome')',.025) if cutoff == `outcome'
	
	replace pre_lb = pre_val-se*invttail(`=scalar(edf_r`outcome')',.025) if cutoff == `outcome'
	replace pre_ub = pre_val+se*invttail(`=scalar(edf_r`outcome')',.025) if cutoff == `outcome'
}

twoway (bar pre_val cutoff, barwidth(3) lcolor(navy*.4) fcolor(navy*.4)) ///
	(bar init_v cutoff, barwidth(5) lcolor(black) fcolor(none)) ///
	(rcap pre_ub pre_lb cutoff, lcolor(black) lwidth(vthin)) ///
	, legend(order(1 "After Mean Impact" 2 "Pre-EITC") rows(1)) graphregion(color(white)) ///
	xlabel(0(5)40)
	
//I want the cutoff to be at the lower end of the bar and in thousands of dollars
gen cutoff_mod = cutoff*1000+2500
lab def cutoffs 2500 "Under $5k" 7500 "$5-10K" 12500 "$10-15K" 17500 "$15-20K" 22500 "$20-25K" 27500 "$25-30K" 32500 "$30-35K" 37500 "$35-40K" 42500 "$40-50K", replace
lab val cutoff_mod cutoffs


//I also want to plot the EITC schedule
gen eitc_cutoff2 = .
replace eitc_cutoff2 = 0 if _n == 1
replace eitc_cutoff2 = 12590 if _n == 2
replace eitc_cutoff2 = 16450 if _n == 3
replace eitc_cutoff2 = 40363 if _n == 4

gen eitc_amnt2 = .
replace eitc_amnt2 = 0 if _n == 1
replace eitc_amnt2 = 5036 if _n == 2
replace eitc_amnt2 = 5036 if _n == 3
replace eitc_amnt2 = 0 if _n == 4

gen eitc_cutoff3 = .
replace eitc_cutoff3 = 0 if _n == 1
replace eitc_cutoff3 = 12590 if _n == 2
replace eitc_cutoff3 = 16450 if _n == 3
replace eitc_cutoff3 = 43352 if _n == 4

gen eitc_amnt3 = .
replace eitc_amnt3 = 0 if _n == 1
replace eitc_amnt3 = 5666 if _n == 2
replace eitc_amnt3 = 5666 if _n == 3
replace eitc_amnt3 = 0 if _n == 4


lab val eitc_cutoff2 cutoffs
lab val eitc_cutoff3 cutoffs

twoway (bar beta cutoff_mod, barwidth(5000) lcolor(black) fcolor(none) yaxis(1)) ///
	(rcap m_ub m_lb cutoff_mod, lcolor(black) lwidth(vthin) yaxis(1)) ///
	(line eitc_amnt3 eitc_cutoff3, lwidth(thin) lpattern(__) lcolor(navy*.6) yaxis(2) ///
	text(.89 33300  "3+ Eligible Children", size(vsmall))) ///
	(pcarrowi .86 33300 .69 31000 , color(black)) ///
	(line eitc_amnt2 eitc_cutoff2, lwidth(thin) lpattern(_-_) lcolor(maroon*.6) yaxis(2) ///
	text(.35 42000  "2 Eligible Children", size(vsmall))) ///
	(pcarrowi .32 42000 .15 38000 , color(black)) ///
	, legend(off /*order(1 "Impact of Out-of-State Friend Exposure at Mean") rows(1)*/) graphregion(color(white)) ///
	xlabel(2500(5000)42500, valuelabels labsize(small)) xtitle("") ytitle(Percentage Points, axis(1)) ///
	yscale(range(-4000 6000) axis(2)) ylabel(0(2000)6000, axis(2)) ytitle("EITC Amount ($)", axis(2))
	
cd $output
graph export impact_earningdist.png, replace

twoway (bar beta cutoff_mod, barwidth(5000) lcolor(black) fcolor(none) yaxis(1)) ///
	(rcap m_ub m_lb cutoff_mod, lcolor(black) lwidth(vthin) yaxis(1)) ///
	(line eitc_amnt3 eitc_cutoff3, lwidth(thin) lpattern(__) lcolor(black*.6) yaxis(2) ///
	text(.89 33300  "3+ Eligible Children", size(vsmall))) ///
	(pcarrowi .86 33300 .69 31000 , color(black)) ///
	(line eitc_amnt2 eitc_cutoff2, lwidth(thin) lpattern(_-_) lcolor(black*.4) yaxis(2) ///
	text(.35 42000  "2 Eligible Children", size(vsmall))) ///
	(pcarrowi .32 42000 .15 38000 , color(black)) ///
	, legend(off /*order(1 "Impact of Out-of-State Friend Exposure at Mean") rows(1)*/) graphregion(color(white)) ///
	xlabel(2500(5000)42500, valuelabels labsize(small)) xtitle("") ytitle(Percentage Points, axis(1)) ///
	yscale(range(-4000 6000) axis(2)) ylabel(0(2000)6000, axis(2)) ytitle("EITC Amount ($)", axis(2))
	
cd $output
graph export impact_earningdist_bw.png, replace

/******************************
Figure 5. Event Study Impacts on EITC claiming
******************************/
cd $data
use state_eitcrates1985_2018, clear

gen anyeitc = eitc_pct>0
gen introduce = anyeitc == 1 & anyeitc[_n-1] == 0 & stfips == stfips[_n-1]
keep if introduce == 1 & year>=2003 & year<=2011
gen n = _n
keep stfips year stateab eitc_pct introduce n
reshape wide stfips year stateab eitc_pct, i(introduce) j(n)

cd $data
save steitc_events2003_2011, replace

cd $fbdata
insheet using County_County.csv, clear
/*I want to interpret sci as the number of friends or links. To do this, I am 
going to multiply by 400 so that .0025 (the smallest non zero) is equal to one. 
This might not actually be the number of links but it is some factor of it.*/

gen fb_links = sci*400

rename own_county ctyfips
cd $data
merge m:1 ctyfips using county_pop_centroids2010
drop if _m == 2
drop _m
rename population pop_2010

rename friend_county f_ctyfips 
gen f_stfips = floor(f_ctyfips/1000)
gen stfips = floor(ctyfips/1000)

gen introduce = 1
cd $data
merge m:1 introduce using steitc_events2003_2011
drop _m 
drop if stfips == f_stfips
bys ctyfips: egen tot_ostatef = sum(fb_links)
forval i = 1/12 {
	gen fb_share`i' = fb_links/tot_ostatef if f_stfips == stfips`i'
	gen fblinks_`i' = fb_links if f_stfips == stfips`i'
}
collapse (sum) fb_share* fblinks_* , by(ctyfips pop_2010 stfips* year* eitc_pct* stateab*)

rename stfips o_stfips
reshape long stfips year eitc_pct stateabbreviation fb_share fblinks_ , i(ctyfips) j(eventnum)
rename stfips exp_stfips

gen eitcexp_fblinkspp = (fblinks_/pop_2010)
//Now I want to expand to have some years before (4) and some years after (4) (I will only have two for the 2011 event
expand 9
rename year event_yr
bys ctyfips eventnum: gen year = _n+event_yr-5
gen t_yr = year-event_yr

cd $data
save events_panel, replace
////////////
cd $data
use cty_eitcnetwork1999_2013, clear

qui reghdfe eitc_rate eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= ., absorb(ctyfips st_yr) vce(cluster stfips)
sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1, de
local mean = r(mean)
loca sd = r(sd)
cd $data
use events_panel, clear
drop if o_stfips == exp_stfips

sum eitcexp_fblinkspp , de 
gen std_eitcexp_fblinkspp = (eitcexp_fblinkspp-`mean')/`sd'

merge m:1 ctyfips year using cty_eitcnetwork1999_2013,
drop if _m == 1 //This is 2014 and 2015, no data
drop _m 


//Create event study
forval yr = 1/4 {
	gen yr_neg`yr' = t_yr == -`yr'
	gen yr_pos`yr' = t_yr == `yr'
}
gen yr_0 = t_yr == 0

forval yr = 1/4 {
	gen fblink_yr_neg`yr' = std_eitcexp_fblinkspp*yr_neg`yr'
	gen fblink_yr_pos`yr' = std_eitcexp_fblinkspp*yr_pos`yr'
}
gen fblink_yr_0 = std_eitcexp_fblinkspp*yr_0

egen event_st_yr = group(eventnum year o_stfips)
egen event_cty = group(eventnum ctyfips)

sort eventnum t_yr o_stfips
drop if o_stfips == exp_stfips //A few counties in Connecticut that have missing stfips

gen eitcnonself_rate = ((eitc_returns-eitc_selfemp)/tot_returns)*100

gen period_t = _n-4 if _n<=8
gen beta_self = .
gen se_self = .
gen beta_nonself = .
gen se_nonself = .

reghdfe eitcself_rate  fblink_yr_neg3 fblink_yr_neg2 fblink_yr_0 fblink_yr_pos1 fblink_yr_pos2 fblink_yr_pos3 fblink_yr_pos4 ///
	$controls  [aw = pop2000] if event_yr <2011 & t_yr>=-3 & t_yr<=4, absorb(event_cty event_st_yr) vce(cluster o_stfips)
	scalar edf_rself = e(df_r)
	forval i = 2/3 {
		replace beta_self = _b[fblink_yr_neg`i'] if period_t == -`i'
		replace se_self = _se[fblink_yr_neg`i'] if period_t == -`i'
		replace beta_self = _b[fblink_yr_pos`i'] if period_t == `i'
		replace se_self = _se[fblink_yr_pos`i'] if period_t == `i'
	}
	replace beta_self = _b[fblink_yr_pos1] if period_t == 1
	replace se_self = _se[fblink_yr_pos1] if period_t == 1
	replace beta_self = _b[fblink_yr_pos4] if period_t == 4
	replace se_self = _se[fblink_yr_pos4] if period_t == 4
	replace beta_self = _b[fblink_yr_0] if period_t == 0
	replace se_self = _se[fblink_yr_0] if period_t == 0
	
	replace beta_self = 0 if period_t == -1
	replace se_self = 0 if period_t == -1
	
reghdfe eitcnonself_rate  fblink_yr_neg3 fblink_yr_neg2 fblink_yr_0 fblink_yr_pos1 fblink_yr_pos2 fblink_yr_pos3 fblink_yr_pos4 ///
	$controls  [aw = pop2000] if event_yr <2011 & t_yr>=-3 & t_yr<=4, absorb(event_cty event_st_yr) vce(cluster o_stfips)
	scalar edf_rnonself = e(df_r)
	forval i = 2/3 {
		replace beta_nonself = _b[fblink_yr_neg`i'] if period_t == -`i'
		replace se_nonself = _se[fblink_yr_neg`i'] if period_t == -`i'
		replace beta_nonself = _b[fblink_yr_pos`i'] if period_t == `i'
		replace se_nonself = _se[fblink_yr_pos`i'] if period_t == `i'
	}
	replace beta_nonself = _b[fblink_yr_pos1] if period_t == 1
	replace se_nonself = _se[fblink_yr_pos1] if period_t == 1
	replace beta_nonself = _b[fblink_yr_pos4] if period_t == 4
	replace se_nonself = _se[fblink_yr_pos4] if period_t == 4
	replace beta_nonself = _b[fblink_yr_0] if period_t == 0
	replace se_nonself = _se[fblink_yr_0] if period_t == 0
	
	replace beta_nonself = 0 if period_t == -1
	replace se_nonself = 0 if period_t == -1
	
	
gen self_lb = beta_self-se_self*invttail(`=scalar(edf_rself)',.025)
gen self_ub = beta_self+se_self*invttail(`=scalar(edf_rself)',.025)

gen nonself_lb = beta_nonself-se_nonself*invttail(`=scalar(edf_rnonself)',.025)
gen nonself_ub = beta_nonself+se_nonself*invttail(`=scalar(edf_rnonself)',.025)

cd $output
twoway (scatter beta_self period_t, connect(line) mcolor(navy) lcolor(navy) yline(0, lcolor(black) lwidth(vthin))) ///
	(rcap self_lb self_ub period_t, lcolor(navy*.6) lwidth(vthin)) ///
	, legend(order(1 "EITC Returns with Self Employment" ) rows(1) symxsize(6) size(small)) ///
	graphregion(color(white)) xtitle(Years Relative to Change) ylabel(-.8(.2).8) xlabel(-3(1)4) ytitle(As Percent of Total Returns) 
graph export eitcselfrate_event.png, replace

cd $output
twoway (scatter beta_nonself period_t, connect(line) mcolor(maroon*.6) lcolor(maroon*.6) yline(0, lcolor(black) lwidth(vthin))) ///
	(rcap nonself_lb nonself_ub period_t, lcolor(maroon*.4) lwidth(vthin)) ///
	, legend(order(1 "EITC Returns without Self Employment") rows(1) symxsize(6) size(small)) ///
	graphregion(color(white)) xtitle(Years Relative to Change)  ylabel(-.8(.2).8) xlabel(-3(1)4) ytitle(As Percent of Total Returns) 
graph export eitcnonselfrate_event.png, replace


cd $output
twoway (scatter beta_self period_t, connect(line) mcolor(black*.8) lcolor(black*.8) yline(0, lcolor(black) lwidth(vthin))) ///
	(rcap self_lb self_ub period_t, lcolor(black*.6) lwidth(vthin)) ///
	, legend(order(1 "EITC Returns with Self Employment" ) rows(1) symxsize(6) size(small)) ///
	graphregion(color(white)) xtitle(Years Relative to Change) ylabel(-.8(.2).8) xlabel(-3(1)4) ytitle(As Percent of Total Returns) 
graph export eitcselfrate_event_bw.png, replace

cd $output
twoway (scatter beta_nonself period_t, connect(line) mcolor(black*.4) lcolor(black*.4) yline(0, lcolor(black) lwidth(vthin))) ///
	(rcap nonself_lb nonself_ub period_t, lcolor(black*.3) lwidth(vthin)) ///
	, legend(order(1 "EITC Returns without Self Employment") rows(1) symxsize(6) size(small)) ///
	graphregion(color(white)) xtitle(Years Relative to Change)  ylabel(-.8(.2).8) xlabel(-3(1)4) ytitle(As Percent of Total Returns) 
graph export eitcnonselfrate_event_bw.png, replace


//What if I mutli-way cluster?
/*
reghdfe eitcself_rate  fblink_yr_neg3 fblink_yr_neg2 fblink_yr_0 fblink_yr_pos1 fblink_yr_pos2 fblink_yr_pos3 fblink_yr_pos4 ///
	$controls  [aw = pop2000] if event_yr <2011 & t_yr>=-3 & t_yr<=4, absorb(event_cty event_st_yr) vce(cluster o_stfips exp_stfips)
reghdfe eitcnonself_rate  fblink_yr_neg3 fblink_yr_neg2 fblink_yr_0 fblink_yr_pos1 fblink_yr_pos2 fblink_yr_pos3 fblink_yr_pos4 ///
	$controls  [aw = pop2000] if event_yr <2011 & t_yr>=-3 & t_yr<=4, absorb(event_cty event_st_yr) vce(cluster o_stfips exp_stfips)
//What if I wild Bootstrap multi-way cluster?
/*need to set matsize and maxvar befoe this*/
areg eitcself_rate  fblink_yr_neg3 fblink_yr_neg2 fblink_yr_0 fblink_yr_pos1 fblink_yr_pos2 fblink_yr_pos3 fblink_yr_pos4 ///
	$controls i.event_st_yr [aw = pop2000] if event_yr <2011 & t_yr>=-3 & t_yr<=4, absorb(event_cty ) vce(cluster o_stfips exp_stfips)
boottest {fblink_yr_neg3 = 0} {fblink_yr_neg2 = 0} {fblink_yr_0 = 0} {fblink_yr_pos1 = 0} {fblink_yr_pos2 = 0 } {fblink_yr_pos3 = 0} {fblink_yr_pos4 = 0}, weighttype(webb) nograph seed(59485)
reghdfe eitcnonself_rate  fblink_yr_neg3 fblink_yr_neg2 fblink_yr_0 fblink_yr_pos1 fblink_yr_pos2 fblink_yr_pos3 fblink_yr_pos4 ///
	$controls i.event_st_yr [aw = pop2000] if event_yr <2011 & t_yr>=-3 & t_yr<=4, absorb(event_cty) vce(cluster o_stfips exp_stfips)
boottest {fblink_yr_neg3 = 0} {fblink_yr_neg2 = 0} {fblink_yr_0 = 0} {fblink_yr_pos1 = 0} {fblink_yr_pos2 = 0 } {fblink_yr_pos3 = 0} {fblink_yr_pos4 = 0}, weighttype(webb) nograph seed(40684)
*/
/******************************
Figure 6. Google Search Interest by Exposure
******************************/
cd $data
use dma_eitcgoogletrend_network04_17, clear
sum eitcexp_fblinkspp, de
gen sd_eitcexp = (eitcexp_fblinkspp-r(mean))/r(sd)

gen haveeitc = eitc_pct>0
foreach w in eic eitc taxcredit refund earnedincomecredit earnedincometaxcredit selfemployed schedulec schedulee schedulef {
	replace `w' = 0 if `w' == .
	gen d`w' = `w'
	replace d`w' = 1 if `w' == 0
	
	//now standarize
	bys dma_id: egen `w'_2004 = max(d`w'*(year == 2004 & month == 1))
	
	gen `w'_std = (d`w'/`w'_2004)
}

egen dmaid = group(dma_id)
egen dma_yr = group(dma_id year)
egen st_yr = group(stfips year)
gen tax_time = inlist(month,1,2,3,4)
gen sd_eitcexp_taxtime = sd_eitcexp*tax_time

forval i = 1/11 {
	gen month`i' = month == `i'
	gen sd_eitcexp_month`i' = sd_eitcexp*month`i'
}


gen lab_month = _n if _n<=11
lab def monthlab 1 "J" 2 "F" 3 "M" 4 "A" 5 "M" 6 "J" 7 "J" 8 "A" 9 "S" 10 "O" 11 "N" 12 "D"
label val lab_month monthlab
foreach outcome in eic eitc earnedincomecredit earnedincometaxcredit selfemployed schedulec {
	gen b_`outcome' = .
	gen se_`outcome' = .
	gen lb_`outcome' = .
	gen ub_`outcome' = .
	reghdfe `outcome' sd_eitcexp sd_eitcexp_month*, absorb(dmaid month year) vce(cluster dmaid) //results similar if I index all time series to the same initial period
	scalar edf_r`outcome' = e(df_r)
	forval m = 1/11 {
		replace b_`outcome' = _b[sd_eitcexp_month`m'] if lab_month == `m'
		replace se_`outcome' = _se[sd_eitcexp_month`m'] if lab_month == `m'
		replace lb_`outcome' = _b[sd_eitcexp_month`m']-_se[sd_eitcexp_month`m']*invttail(`=scalar(edf_r`outcome')',.025) if lab_month == `m'
		replace ub_`outcome' = _b[sd_eitcexp_month`m']+_se[sd_eitcexp_month`m']*invttail(`=scalar(edf_r`outcome')',.025) if lab_month == `m'
	}
}

twoway (scatter b_eic lab_month, mcolor(navy) yline(0, lcolor(black))) ///
	(rcap lb_eic ub_eic lab_month, lcolor(black) lwidth(thin)) ///
	, legend(off) graphregion(color(white)) xtitle(Month) xlabel(1(1)11, valuelabel) ///
	ytitle(Coefficient on Network Exposure) subtitle(`"Term: "eic" "')
	
cd $output
graph export google_regeic.png, replace

twoway (scatter b_eitc lab_month if lab_month<=11, mcolor(navy) yline(0, lcolor(black))) ///
	(rcap lb_eitc ub_eitc lab_month, lcolor(black) lwidth(thin)) ///
	, legend(off) graphregion(color(white)) xtitle(Month) xlabel(1(1)11, valuelabel) ///
	ytitle(Coefficient on Network Exposure) subtitle(`"Term: "eitc" "')
	
cd $output
graph export google_regeitc.png, replace

twoway (scatter b_earnedincomecredit lab_month if lab_month<=11, mcolor(navy) yline(0, lcolor(black))) ///
	(rcap lb_earnedincomecredit ub_earnedincomecredit lab_month, lcolor(black) lwidth(thin)) ///
	, legend(off) graphregion(color(white)) xtitle(Month) xlabel(1(1)11, valuelabel) ///
	ytitle(Coefficient on Network Exposure) subtitle(`"Term: "earned income credit" "')
cd $output
graph export google_regearnedincomecredit.png, replace

twoway (scatter b_earnedincometaxcredit lab_month if lab_month<=11, mcolor(navy) yline(0, lcolor(black))) ///
	(rcap lb_earnedincometaxcredit ub_earnedincometaxcredit lab_month, lcolor(black) lwidth(thin)) ///
	, legend(off) graphregion(color(white)) xtitle(Month) xlabel(1(1)11, valuelabel) ///
	ytitle(Coefficient on Network Exposure) subtitle(`"Term: "earned income tax credit" "')
cd $output
graph export google_regearnedincometaxcredit.png, replace

twoway (scatter b_selfemployed lab_month if lab_month<=11, mcolor(navy) yline(0, lcolor(black))) ///
	(rcap lb_selfemployed ub_selfemployed lab_month, lcolor(black) lwidth(thin)) ///
	, legend(off) graphregion(color(white)) xtitle(Month) xlabel(1(1)11, valuelabel) ///
	ytitle(Coefficient on Network Exposure) subtitle(`"Term: "self employed" "')
cd $output
graph export google_regselfemployed.png, replace
	
twoway (scatter b_schedulec lab_month if lab_month<=11, mcolor(navy) yline(0, lcolor(black))) ///
	(rcap lb_schedulec ub_schedulec lab_month, lcolor(black) lwidth(thin)) ///
	, legend(off) graphregion(color(white)) xtitle(Month) xlabel(1(1)11, valuelabel) ///
	ytitle(Coefficient on Network Exposure) subtitle(`"Term: "schedule c" "')
cd $output
graph export google_regschedulec.png, replace

//Now save in black and white
twoway (scatter b_eic lab_month, mcolor(black) yline(0, lcolor(black))) ///
	(rcap lb_eic ub_eic lab_month, lcolor(black) lwidth(thin)) ///
	, legend(off) graphregion(color(white)) xtitle(Month) xlabel(1(1)11, valuelabel) ///
	ytitle(Coefficient on Network Exposure) subtitle(`"Term: "eic" "')
	
cd $output
graph export google_regeic_bw.png, replace

twoway (scatter b_eitc lab_month if lab_month<=11, mcolor(black) yline(0, lcolor(black))) ///
	(rcap lb_eitc ub_eitc lab_month, lcolor(black) lwidth(thin)) ///
	, legend(off) graphregion(color(white)) xtitle(Month) xlabel(1(1)11, valuelabel) ///
	ytitle(Coefficient on Network Exposure) subtitle(`"Term: "eitc" "')
	
cd $output
graph export google_regeitc_bw.png, replace

twoway (scatter b_earnedincomecredit lab_month if lab_month<=11, mcolor(black) yline(0, lcolor(black))) ///
	(rcap lb_earnedincomecredit ub_earnedincomecredit lab_month, lcolor(black) lwidth(thin)) ///
	, legend(off) graphregion(color(white)) xtitle(Month) xlabel(1(1)11, valuelabel) ///
	ytitle(Coefficient on Network Exposure) subtitle(`"Term: "earned income credit" "')
cd $output
graph export google_regearnedincomecredit_bw.png, replace

twoway (scatter b_earnedincometaxcredit lab_month if lab_month<=11, mcolor(black) yline(0, lcolor(black))) ///
	(rcap lb_earnedincometaxcredit ub_earnedincometaxcredit lab_month, lcolor(black) lwidth(thin)) ///
	, legend(off) graphregion(color(white)) xtitle(Month) xlabel(1(1)11, valuelabel) ///
	ytitle(Coefficient on Network Exposure) subtitle(`"Term: "earned income tax credit" "')
cd $output
graph export google_regearnedincometaxcredit_bw.png, replace

twoway (scatter b_selfemployed lab_month if lab_month<=11, mcolor(black) yline(0, lcolor(black))) ///
	(rcap lb_selfemployed ub_selfemployed lab_month, lcolor(black) lwidth(thin)) ///
	, legend(off) graphregion(color(white)) xtitle(Month) xlabel(1(1)11, valuelabel) ///
	ytitle(Coefficient on Network Exposure) subtitle(`"Term: "self employed" "')
cd $output
graph export google_regselfemployed_bw.png, replace
	
twoway (scatter b_schedulec lab_month if lab_month<=11, mcolor(black) yline(0, lcolor(black))) ///
	(rcap lb_schedulec ub_schedulec lab_month, lcolor(black) lwidth(thin)) ///
	, legend(off) graphregion(color(white)) xtitle(Month) xlabel(1(1)11, valuelabel) ///
	ytitle(Coefficient on Network Exposure) subtitle(`"Term: "schedule c" "')
cd $output
graph export google_regschedulec_bw.png, replace

/******************************
************TABLES*************
******************************/

/******************************
Table 1. Summary Statistics (Pre-Treatment Predictive Power)
******************************/
cd $data
use cty_eitcnetwork1999_2013, clear

gen eitcnonself_rate = ((eitc_returns-eitc_selfemp)/tot_returns)*100

qui reghdfe eitc_rate eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= ., absorb(ctyfips st_yr) vce(cluster stfips)
	gen esamp = e(sample) == 1

foreach e in lshs hs scoll coll {
	gen popshare`e' = pop_`e'2000 if year == 2000
	replace popshare`e' = pop_`e'2013 if year == 2013
}

//Create measure change from 2000 to 2013
gen t = eitcexp_fblinkspp if year == 2000
bys ctyfips: egen fblink2000 = max(t)
drop t
gen t = eitcexp_fblinkspp if year == 2013
bys ctyfips: egen fblink2013 = max(t)
drop t
gen delta_fblinks00_13 = fblink2013-fblink2000
gen pop2000_k = pop2000/1000

bys stfips: egen p50_deltafblinks = pctile(delta_fblinks00_13), p(50)
gen below_stmean = delta_fblinks00_13<=p50_deltafblinks
//Tex file
local vars "eitcexp_fblinkspp eitc_rate eitcnonself_rate eitcself_rate ave_eitc popshare_female popshare_nhwhite popshare_nhblack popshare_hisp popshare_nhother popsharelshs popsharehs popsharescoll popsharecoll urate ave_jobearn"
local var_lab = `" "Out-of-State Friends Exposed to State EITC in 2000" "Percent of Returns with EITC" "\hspace*{6mm} No-Self Employment" "\hspace*{6mm} Self Employment" "Average EITC Amount" "Percent Female" "Percent NH White" "Percent NH Black" "Percent Hispanic" "Percent NH Other" "Percent Less HS" "Percent HS" "Percent Some College" "Percent College" "Unemployment Rate" "Average Job Earnings" "'
local num : word count `vars'
cd $output
cap file close sumstat
file open sumstat using table_sumstat.tex, write replace
file write sumstat "\begin{tabular}{lccccc}" _n
file write sumstat "\toprule" _n
file write sumstat "\toprule" _n
file write sumstat "& \multicolumn{2}{c}{Mean in 2000} & & \multicolumn{2}{c}{\$\Delta$ from 2000 to 2013} \\" _n
file write sumstat "\cmidrule{2-3} \cmidrule{5-6}" _n
file write sumstat "& \multicolumn{2}{c}{Change in Number of Friends Exposed} & & \multicolumn{2}{c}{Change in Number of Friends Exposed} \\" _n
file write sumstat "& Below State Median & Above State Median & & Below State Median & Above State Median \\" _n
file write sumstat "& (1) & (2) & &  (3) & (4) \\ " _n
file write sumstat "\midrule" _n
forval i = 1/`num' {
	local var : word `i' of `vars'
	local varl : word `i' of `var_lab'
	file write sumstat "`varl'"
	reg `var' below_stmean [aw = pop2000] if esamp == 1 & year == 2000, vce(cluster stfips)
	file write sumstat " &" %7.2f (_b[_cons]+_b[below_stmean]) " &" %7.2f (_b[_cons])
	
	file write sumstat " &"
	gen tt = `var' if year == 2000
	bys ctyfips: egen t2000 = max(tt)
	drop tt
	gen tt = `var' if year == 2013
	bys ctyfips: egen t2013 = max(tt)
	gen dd = t2013-t2000
	drop tt
	reg dd below_stmean [aw = pop2000] if esamp == 1 & year == 2000, vce(cluster stfips)
	drop dd t2000 t2013
	file write sumstat " &" %7.2f (_b[_cons]+_b[below_stmean]) " &" %7.2f (_b[_cons])
	file write sumstat "\\"
	if "`var'" == "ave_eitc" {
		file write sumstat "\midrule"
	}
	file write sumstat _n
	
}
file write sumstat _n "Observations"
foreach b in 0 1 {
	count if esamp == 1 & below_stmean == `b' & year == 2000
	file write sumstat " &" %9.0gc (r(N)) _n
}
file write sumstat " &"
foreach b in 0 1 {
	count if esamp == 1 & below_stmean == `b' & year == 2000
	file write sumstat " &" %9.0gc (r(N)) _n
}
file write sumstat "\\" _n
file write sumstat "\bottomrule" _n
file write sumstat "\bottomrule" _n
file write sumstat "\end{tabular}"
file close sumstat


/******************************
Table 2. Direct Impact of State EITC Expansions on EITC Claiming Behavior
******************************/
cd $data
use cty_eitcnetwork1999_2013, clear

gen eitcnonself_rate = ((eitc_returns-eitc_selfemp)/tot_returns)*100
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	gen ln`outcome' = ln(`outcome')
}
replace eitc_pct = eitc_pct/10

gen haveeitc = eitc_pct >0
gen t = eitc_rate if year == 1999
bys ctyfips: egen eitc_rate1999 = max(t)

gen haveeitc_pctrange = haveeitc*eitc_rate1999

//Effect on Levels
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	reghdfe `outcome' haveeitc_pctrange $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	qui sum `outcome' [aw = pop2000] if e(sample) == 1
	scalar dmean_`outcome' = r(mean)
	qui sum eitc_rate1999 [aw = pop2000] if e(sample) == 1
	scalar imean_`outcome' = r(mean)
}	

cd $output
cap file close regout
file open regout using tab_directstateeitc_instate.tex, write replace
//Header
file write regout "\begin{tabular}{lccccc}" _n
file write regout "\toprule" _n
file write regout "\toprule" _n
file write regout "& \multicolumn{3}{c}{Percent of Returns with EITC} &  \\" _n
file write regout "\cmidrule{2-4}" _n
file write regout "& & & & Percent of EITC \\" _n
file write regout "&  Any EITC & No Self Employment & Self Employment & Returns with Self Emp.  & Average EITC \\" _n
file write regout "&  & (No Schedule C, E, or F) & (Schedule C, E, or F) &  (Schedule C, E, or F) & Amount \\" _n
file write regout "& (1) & (2) & (3) & (4) & (5) \\" _n
file write regout "\midrule" _n
file write regout "\\" _n
//Table
file write regout "Any State EITC*Share HH" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %7.2f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\ \hspace{3mm}in 1999 Claiming EITC" _n
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
file write regout "& & & &  \\" _n
//Statistics
file write regout "Dependent Mean" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %5.1f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Mean 1999 EITC Rate" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %5.1f (`=scalar(imean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}  
file write regout "\\" _n
file write regout "\bottomrule" _n
file write regout "\bottomrule" _n
file write regout "\end{tabular}"
file close regout

/******************************
Table 3. Impact of Social Network Exposure on EITC Claiming Behavior
******************************/
cd $data
use cty_eitcnetwork1999_2013, clear

gen eitcnonself_rate = ((eitc_returns-eitc_selfemp)/tot_returns)*100
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	gen ln`outcome' = ln(`outcome')
}
reghdfe eitc_rate eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
sum eitcexp_fblinkspp [aw = pop2000] if  e(sample) == 1, de
gen std_eitcexp_fblinkspp = (eitcexp_fblinkspp-r(mean))/r(sd)
	
//Effect on Levels
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	reghdfe `outcome' std_eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	qui sum `outcome' [aw = pop2000] if e(sample) == 1
	scalar dmean_`outcome' = r(mean)
	qui sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1
	scalar imean_`outcome' = r(mean)
}	

cd $output
cap file close regout
file open regout using tab_baseline.tex, write replace
//Header
file write regout "\begin{tabular}{lccccc}" _n
file write regout "\toprule" _n
file write regout "\toprule" _n
file write regout "& \multicolumn{3}{c}{Percent of Returns with EITC} &  \\" _n
file write regout "\cmidrule{2-4}" _n
file write regout "& & & & Percent of EITC \\" _n
file write regout "&  Any EITC & No Self Employment & Self Employment & Returns with Self Emp. & Average EITC \\" _n
file write regout "&  & (No Schedule C, E, or F) & (Schedule C, E, or F) &  (Schedule C, E, or F) & Amount \\" _n
file write regout "& (1) & (2) & (3) & (4) & (5) \\" _n
file write regout "\midrule" _n
file write regout " \\" _n
//Table
file write regout "Out-of-State Friends per person" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %7.2f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "Exposed to State EITC (Standardized)" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
file write regout "& & & & \\" _n
//Statistics
file write regout "Dependent Mean" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %5.1f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}  
file write regout "\\" _n
file write regout "\bottomrule" _n
file write regout "\bottomrule" _n
file write regout "\end{tabular}"
file close regout


/**********************
Table 4. Impact on Reported Self Employment in ACS
**********************/
cd $data
use cty_eitcnetwork1999_2013, clear

qui reghdfe eitc_rate eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1, de
local mean = r(mean)
loca sd = r(sd)

cd $data
use  hh_selfemp_network05_17, clear

egen st_yr = group(stfips year)
egen st_cz = group(stfips czone)
gen stdcty_eitcexp_fblinkspp = (eitcexp_fblinkspp-`mean')/`sd'

gen tot_ekids0 = tot_numekids == 0
gen tot_ekids1 = tot_numekids == 1
gen tot_ekids2 = tot_numekids == 2
gen tot_ekids3plus = tot_numekids >=3 & tot_numekids ~= .

gen f_negbusinc = f_incbus002017 <0
gen f_posbusinc = f_incbus002017 >0

reghdfe f_wage_worker stdcty_eitcexp_fblinkspp $acscontrols lshs somcoll coll advcoll single_mominfam tot_ekids* [aw = czhhwt] , absorb(st_cz st_yr) vce(cluster stfips)
gen samp = e(sample) == 1

foreach outcome in f_anyemployment f_wage_worker f_selfemployed f_self_employednotinc f_self_employedinc f_posbusinc f_negbusinc {
	reghdfe `outcome' stdcty_eitcexp_fblinkspp $acscontrols lshs somcoll coll advcoll single_mominfam tot_ekids* [aw = czhhwt] if samp == 1, absorb(st_cz st_yr) vce(cluster stfips)
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	qui sum `outcome' if e(sample) == 1
	scalar dmean_`outcome' = r(mean)
}

foreach outcome in f_incwage2017 f_incbus002017 {
	reghdfe `outcome' stdcty_eitcexp_fblinkspp $acscontrols lshs somcoll coll advcoll single_mominfam tot_ekids* [aw = czhhwt] if samp == 1 & f_incbus002017~= 0, absorb(st_cz st_yr) vce(cluster stfips)
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	qui sum `outcome' if e(sample) == 1
	scalar dmean_`outcome' = r(mean)
}

cd $output
cap file close regout
file open regout using table_acsindemployment.tex, write replace
//Header
file write regout "\begin{tabular}{lccccccccc}" _n
file write regout "\toprule" _n
file write regout "\toprule" _n
file write regout "&  & & & & & & &  \multicolumn{2}{c}{Households with Business Income} \\" _n
file write regout "\cmidrule{9-10}" _n
file write regout "& Any & Any Wage & Any Self & Non-Incorporated & Incorporated & Positive & Negative & Wage & Business  \\" _n
file write regout "& Employment & Employment & Employment & Self-Employment & Self-Employment & Business Income & Business Income & Income & Income  \\" _n
file write regout "& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) & (9) \\" _n
file write regout "\midrule" _n
file write regout " \\" _n
//Table
file write regout "Out-of-State Friends per person" 
foreach outcome in f_anyemployment f_wage_worker f_selfemployed f_self_employednotinc f_self_employedinc f_posbusinc f_negbusinc f_incwage2017 f_incbus002017  {
	if inlist("`outcome'","f_incwage2017", "f_incbus002017") {
		file write regout "&" %7.2f (b_`outcome'[1,1])
	}
	if inlist("`outcome'","f_negbusinc") {
		file write regout "&" %7.4f (b_`outcome'[1,1])
	}
	if inlist("`outcome'","f_anyemployment","f_wage_worker","f_selfemployed","f_self_employednotinc","f_self_employedinc","f_posbusinc") {
		file write regout "&" %7.3f (b_`outcome'[1,1])
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "Exposed to State EITC (Standardized)" 
foreach outcome in f_anyemployment f_wage_worker f_selfemployed f_self_employednotinc f_self_employedinc f_posbusinc f_negbusinc f_incwage2017 f_incbus002017 {
	if inlist("`outcome'","f_incwage2017", "f_incbus002017") {
		file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[1,1]) ) ")"
	}
	if inlist("`outcome'","f_negbusinc") {
		file write regout "&" "(" %5.4f ( sqrt(V_`outcome'[1,1]) ) ")"
	}
	if inlist("`outcome'","f_anyemployment","f_wage_worker","f_selfemployed","f_self_employednotinc","f_self_employedinc","f_posbusinc"){
		file write regout "&" "(" %5.3f ( sqrt(V_`outcome'[1,1]) ) ")"
	}
}
file write regout "\\" _n
file write regout "& & & & \\" _n
//Statistics
file write regout "Dependent Mean" 
foreach outcome in f_anyemployment f_wage_worker f_selfemployed f_self_employednotinc f_self_employedinc f_posbusinc f_negbusinc f_incwage2017 f_incbus002017 {
	file write regout "&" %5.2f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in f_anyemployment f_wage_worker f_selfemployed f_self_employednotinc f_self_employedinc f_posbusinc f_negbusinc f_incwage2017 f_incbus002017  {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}  
file write regout "\\" _n
file write regout "\bottomrule" _n
file write regout "\bottomrule" _n
file write regout "\end{tabular}"
file close regout

/****************************************
Appendix Table A4. Heterogeneity in the ACS
****************************************/

gen lowed = lshs == 1 | hs == 1 | somcoll == 1
gen hied = coll == 1 | advcoll == 1
gen smom = single_mominfam == 1
gen ekids = tot_numekids >= 1 & tot_numekids ~= .
gen nokids = tot_numekids == 0


//Heterogeneity
foreach outcome in lowed hied ekids nokids smom {
	if "`outcome'" == "nokids" {
		reghdfe f_selfemployed stdcty_eitcexp_fblinkspp $acscontrols lshs somcoll coll advcoll single_mominfam [aw = czhhwt]  if `outcome' == 1, absorb(st_cz st_yr) vce(cluster stfips)
	}
	if "`outcome'" == "smom" {
		reghdfe f_selfemployed stdcty_eitcexp_fblinkspp $acscontrols lshs somcoll coll advcoll tot_ekids1 tot_ekids2 [aw = czhhwt]  if `outcome' == 1, absorb(st_cz st_yr) vce(cluster stfips)
	}
	if ~inlist("`outcome'","nokids","smom") {
		reghdfe f_selfemployed stdcty_eitcexp_fblinkspp $acscontrols lshs somcoll coll advcoll single_mominfam tot_ekids* [aw = czhhwt]  if `outcome' == 1, absorb(st_cz st_yr) vce(cluster stfips)
	}
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	qui sum f_selfemployed if e(sample) == 1
	scalar dmean_`outcome' = r(mean)
}
cd $output
cap file close regout
file open regout using table_acsindemployment_het.tex, write replace
//Header
file write regout "\begin{tabular}{lccccc}" _n
file write regout "\toprule" _n
file write regout "\toprule" _n
file write regout "&  \multicolumn{5}{c}{Any Self-Employment} \\" _n
file write regout "\cmidrule{2-6}" _n
file write regout "& Some College & College & EITC Eligible & No EITC Eligible  & Single Mother \\" _n
file write regout "& or Less & Degree & Children & Children & in Family Unit  \\" _n
file write regout "& (1) & (2) & (3) & (4) & (5) \\" _n
file write regout "\midrule" _n
file write regout "\\" _n
//Table
file write regout "Out-of-State Friends per person" 
foreach outcome in lowed hied ekids nokids smom {
	file write regout "&" %7.3f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "Exposed to State EITC (Standardized)" 
foreach outcome in lowed hied ekids nokids smom {
	file write regout "&" "(" %5.3f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
file write regout "\\" _n
//Statistics
file write regout "Dependent Mean" 
foreach outcome in lowed hied ekids nokids smom {
	file write regout "&" %5.2f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in lowed hied ekids nokids smom {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}  
file write regout "\\" _n
file write regout "\bottomrule" _n
file write regout "\bottomrule" _n
file write regout "\end{tabular}"
file close regout


/**********************************
***********APPENDIX FIGURES********
**********************************/
/*******************
Appendix Figure A1. Map of EITC Expansion States
*******************/
cd $data
use state_eitcrateswide1985_2018, clear
reshape long yr, i(state) j(year)
replace year = . if yr == .
collapse (min) year (mean) ave_rate = yr, by(stfips)
replace year = 0 if ave_rate == .
tempfile state_rates
save `state_rates'

cd $data
cd shapefiles

use statedb, clear
destring GEOID, gen(stfips)
merge 1:m stfips using `state_rates'

cd $data
cd shapefiles
spmap year using statecoord if ~inlist(stfips,2,15,60,66,69,72,78), id(_ID) ///
	clnumber(40) fcolor(white ebblue*.2 ebblue*.3 ebblue*.4 ebblue*.5 ebblue*.6 ebblue*.7 ebblue*.8 ebblue*.9 ebblue ebblue*1.1 ebblue*1.2 ebblue*1.3 ebblue*1.4 ebblue*1.5 ebblue*1.6 ebblue*1.7) subtitle(Year Introduced)  saving(eitcrate_states.png, replace) ///
	legend( size(*1.2) label(2 "None") label(3 "1987")  label(4 "1988")  label(5 "1990")  ///
	label(6 "1994")  label(7 "1997")  label(8 "1998")  label(9 "1999")  label(10 "2000")  ///
	label(11 "2002")  label(12 "2003")  label(13 "2006")  label(14 "2007")  label(15 "2008")  ///
	label(16 "2011")  label(17 "2013")  label(18 "2015"))

cd $output
graph export eitcrate_states.png, replace

/***********************
Appendix Figure A2. Distribution of Out-of-State Friends Exposed to EITC in 2000 and 2013
***********************/
cd $data
use cty_eitcnetwork1999_2013, clear

twoway (hist eitcexp_fblinkspp if year == 2000 & eitcexp_fblinkspp <=30, width(1) fcolor(gray*.5) lcolor(gray*.6) frequency) ///
	(hist eitcexp_fblinkspp if year == 2013 & eitcexp_fblinkspp <=30, width(1) fcolor(none) lcolor(black) frequency) , ///
	graphregion(color(white)) xtitle(Number of Out of State Friends per Person Exposed to State EITC) ///
	ytitle(Number of Counties) legend(order(1 "in 2000" 2 "in 2013") rows(1)) xlabel(0(5)30)
cd $output
graph export hist_expf_pp.png, replace

/*The variation I am exploiting is within a state, so remove th cross state variation in each year*/
reghdfe eitcexp_fblinkspp[aw = pop2000], absorb(st_yr) residuals(hat_eitcexp_fblinkspp)
twoway (hist hat_eitcexp_fblinkspp if year == 2000 & hat_eitcexp_fblinkspp <=30, width(1) fcolor(gray*.5) lcolor(gray*.6) frequency) ///
	(hist hat_eitcexp_fblinkspp if year == 2013 & hat_eitcexp_fblinkspp <=30, width(1) fcolor(none) lcolor(black) frequency) , ///
	graphregion(color(white)) xtitle(Number of Out of State Friends per Person Exposed to State EITC) ///
	ytitle(Number of Counties) legend(order(1 "in 2000" 2 "in 2013") rows(1)) xlabel(-20(5)30)
cd $output
	graph export hist_hatexpf_pp.png, replace

/***************
How highly correlated?
***************/

keep if inlist(year,2000,2013)
keep ctyfips year pctany_steitc steitc_pct
reshape wide pctany_steitc steitc_pct, i(ctyfips) j(year)

twoway (scatter pctany_steitc2013 pctany_steitc2000), xtitle("Share of Out of State Friends exposed to State EITC in 2000", size(small) ) ///
	ytitle("Share of Out of State Friends exposed to State EITC in 2010", size(small))  ///
	graphregion(color(white))
	
twoway (scatter steitc_pct2013 steitc_pct2000), xtitle("Average State EITC Rate Out of State Friends Exposed to in 2000", size(small) ) ///
	ytitle("Average State EITC Rate Out of State Friends Exposed to in 2010", size(small))  ///
	graphregion(color(white))


/****************************
Appendix Figure A3. Direct Effect Event Studies
*************************/
cd $data
use cty_eitcnetwork1999_2013, clear
foreach i in 0 5 10 15 20 25 30 35 40 {
	gen pcteitc_`i' = (eagi`i'/eitc_returns)*100
}
gen eitcnonself_rate = ((eitc_returns-eitc_selfemp)/tot_returns)*100
gen haveeitc = eitc_pct >0
gen pct_eitcrange = ((tagi0+tagi5+tagi10+tagi15+tagi20+tagi25+tagi30+tagi35)/tot_returns)

gen t = eitc_rate if year == 1999
bys ctyfips: egen eitc_rate1999 = max(t)


tempfile claim_rate
save `claim_rate'
cd $data

use events_panel, clear
keep if o_stfips == exp_stfips

merge 1:1 ctyfips year using `claim_rate',
drop if _m == 1 //This is 2014 and 2015, no data
drop if _m == 2 //These are counties that don't expand
drop _m 

gen haveeitc_pctrange = haveeitc*eitc_rate1999

//Create event study
forval yr = 1/4 {
	gen yr_neg`yr' = t_yr == -`yr'
	gen yr_pos`yr' = t_yr == `yr'
}
gen yr_0 = t_yr == 0

forval yr = 1/4 {
	gen eitcrange_yr_neg`yr' = eitc_rate1999*yr_neg`yr'
	gen eitcrange_yr_pos`yr' = eitc_rate1999*yr_pos`yr'
}
gen eitcrange_yr_0 = eitc_rate1999*yr_0

gen period_t = _n-4 if _n<=8

foreach i in nonself self {
	gen beta_`i' = .
	gen se_`i' = .
	gen wb_lb_`i' = .
gen wb_ub_`i' = .
areg eitc`i'_rate eitcrange_yr_neg3 eitcrange_yr_neg2 eitcrange_yr_0 eitcrange_yr_pos1 eitcrange_yr_pos2 eitcrange_yr_pos3 eitcrange_yr_pos4 ///
	$controls i.st_yr [aw = pop2000] if event_yr <2011 & t_yr>=-3 & t_yr<=4, absorb(ctyfips ) vce(cluster stfips)
	scalar edf_r`i' = e(df_r)
	forval j = 2/3 {
		replace beta_`i' = _b[eitcrange_yr_neg`j'] if period_t == -`j'
		replace se_`i' = _se[eitcrange_yr_neg`j'] if period_t == -`j'
		replace beta_`i' = _b[eitcrange_yr_pos`j'] if period_t == `j'
		replace se_`i' = _se[eitcrange_yr_pos`j'] if period_t == `j'
	}
	replace beta_`i' = _b[eitcrange_yr_pos1] if period_t == 1
	replace se_`i' = _se[eitcrange_yr_pos1] if period_t == 1
	replace beta_`i' = _b[eitcrange_yr_pos4] if period_t == 4
	replace se_`i' = _se[eitcrange_yr_pos4] if period_t == 4
	replace beta_`i' = _b[eitcrange_yr_0] if period_t == 0
	replace se_`i' = _se[eitcrange_yr_0] if period_t == 0
	
	replace beta_`i' = 0 if period_t == -1
	replace se_`i' = 0 if period_t == -1	
	gen lb_`i' = beta_`i'-se_`i'*invttail(`=scalar(edf_r`i')',.025)
	gen ub_`i' = beta_`i'+se_`i'*invttail(`=scalar(edf_r`i')',.025)
	
	//get wild bootstrapped confidence intervals
	areg eitc`i'_rate eitcrange_yr_neg3 eitcrange_yr_neg2 eitcrange_yr_0 eitcrange_yr_pos1 eitcrange_yr_pos2 eitcrange_yr_pos3 eitcrange_yr_pos4 ///
	$controls i.st_yr [aw = pop2000] if event_yr <2011 & t_yr>=-3 & t_yr<=4, absorb(ctyfips) vce(cluster stfips)
	boottest {eitcrange_yr_neg3 = 0} {eitcrange_yr_neg2 = 0} {eitcrange_yr_0 = 0} {eitcrange_yr_pos1 = 0} {eitcrange_yr_pos2 = 0 } {eitcrange_yr_pos3 = 0} {eitcrange_yr_pos4 = 0}, weighttype(webb) nograph seed(59485)
	forval k = 1/2 {
		matrix ci_`k' = r(CI_`k')
		local l = `k'-4
		replace wb_lb_`i' = ci_`k'[1,1] if period_t == `l'
		replace wb_ub_`i' = ci_`k'[1,2] if period_t == `l'
	}
	forval k = 3/7 {
		matrix ci_`k' = r(CI_`k')
		local l = `k'-3
		replace wb_lb_`i' = ci_`k'[1,1] if period_t == `l'
		replace wb_ub_`i' = ci_`k'[1,2] if period_t == `l'
	}
}
	
cd $output
twoway (scatter beta_self period_t, connect(line) mcolor(navy) lcolor(navy) yline(0, lcolor(black) lwidth(vthin))) ///
		(rcap wb_lb_self wb_ub_self period_t, lcolor(navy*.6) lwidth(vthin)) ///
		, legend(order(1 "EITC Returns with Self Employment") rows(1) symxsize(6) size(small)) ///
		graphregion(color(white)) xtitle(Years Relative to Change) ylabel(-.15(.05).15) xlabel(-3(1)4) ytitle(As Percent of Total Returns) 
	
	graph export direct_eitcselfrate_event.png, replace
	
twoway (scatter beta_nonself period_t, connect(line) mcolor(maroon*.6) lcolor(maroon*.6) yline(0, lcolor(black) lwidth(vthin)) ) ///
		(rcap wb_lb_nonself wb_ub_nonself period_t, lcolor(maroon*.4) lwidth(vthin) ) ///
		, legend(order(1 "EITC Returns without Self Employment") rows(1) symxsize(6) size(small)) ///
		graphregion(color(white)) xtitle(Years Relative to Change) ylabel(-.15(.05).15) xlabel(-3(1)4) ytitle(As Percent of Total Returns) 
	
	graph export direct_eitcnonselfrate_event.png, replace

/******************************
Appendix Figure A4. Impact of Out-of-Friend Exposure on Non-EITC income distribution
******************************/
cd $data
use cty_eitcnetwork1999_2013, clear
foreach i in 0 5 10 15 20 25 30 35 40 {
	gen pcttot_`i' = ((tagi`i'-eagi`i')/(tot_returns-eitc_returns))*100
}

reghdfe eitc_rate eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1, de
gen std_eitcexp_fblinkspp = (eitcexp_fblinkspp-r(mean))/r(sd)

egen rowtotal = rowtotal(pcttot_*)

gen haveeitc = eitc_pct >0
gen t = eitc_rate if year == 1999
bys ctyfips: egen eitc_rate1999 = max(t)
gen haveeitc_pcteitc = haveeitc*eitc_rate1999
drop t

gen cutoff = _n*5-5 if _n<=9
gen beta = .
gen se = .
gen init_v = .
gen pre_lb = .
gen pre_ub = .
gen m_lb = .
gen m_ub = .

//Effect on Levels
foreach outcome in 0 5 10 15 20 25 30 35 40 {
	reghdfe pcttot_`outcome' haveeitc_pcteitc  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
	replace beta = _b[haveeitc_pcteitc] if cutoff == `outcome'
	replace se = _se[haveeitc_pcteitc] if cutoff == `outcome'
	scalar edf_r`outcome' = e(df_r)
	qui sum pcttot_`outcome' [aw = pop2000] if e(sample) == 1
	replace init_v = r(mean) if cutoff == `outcome'
}

egen t = sum(init_v)

gen pre_val = init_v+beta

foreach outcome in 0 5 10 15 20 25 30 35 40 {

	replace m_lb = beta-se*invttail(`=scalar(edf_r`outcome')',.025) if cutoff == `outcome'
	replace m_ub = beta+se*invttail(`=scalar(edf_r`outcome')',.025) if cutoff == `outcome'
	
	replace pre_lb = pre_val-se*invttail(`=scalar(edf_r`outcome')',.025) if cutoff == `outcome'
	replace pre_ub = pre_val+se*invttail(`=scalar(edf_r`outcome')',.025) if cutoff == `outcome'
}

//I want the cutoff to be at the lower end of the bar and in thousands of dollars
gen cutoff_mod = cutoff*1000+2500
lab def cutoffs 2500 "Under $5k" 7500 "$5-10K" 12500 "$10-15K" 17500 "$15-20K" 22500 "$20-25K" 27500 "$25-30K" 32500 "$30-35K" 37500 "$35-40K" 42500 "$40-50K", replace
lab val cutoff_mod cutoffs


//I also want to plot the EITC schedule
gen eitc_cutoff2 = .
replace eitc_cutoff2 = 0 if _n == 1
replace eitc_cutoff2 = 12590 if _n == 2
replace eitc_cutoff2 = 16450 if _n == 3
replace eitc_cutoff2 = 40363 if _n == 4

gen eitc_amnt2 = .
replace eitc_amnt2 = 0 if _n == 1
replace eitc_amnt2 = 5036 if _n == 2
replace eitc_amnt2 = 5036 if _n == 3
replace eitc_amnt2 = 0 if _n == 4

gen eitc_cutoff3 = .
replace eitc_cutoff3 = 0 if _n == 1
replace eitc_cutoff3 = 12590 if _n == 2
replace eitc_cutoff3 = 16450 if _n == 3
replace eitc_cutoff3 = 43352 if _n == 4

gen eitc_amnt3 = .
replace eitc_amnt3 = 0 if _n == 1
replace eitc_amnt3 = 5666 if _n == 2
replace eitc_amnt3 = 5666 if _n == 3
replace eitc_amnt3 = 0 if _n == 4


lab val eitc_cutoff2 cutoffs
lab val eitc_cutoff3 cutoffs

twoway (bar beta cutoff_mod, barwidth(5000) lcolor(black) fcolor(none) yaxis(1)) ///
	(rcap m_ub m_lb cutoff_mod, lcolor(black) lwidth(vthin) yaxis(1)) ///
	(line eitc_amnt3 eitc_cutoff3, lwidth(thin) lpattern(__) lcolor(navy*.6) yaxis(2) ///
	text(.075 33300  "3+ Eligible Children", size(vsmall))) ///
	(pcarrowi .072 33300 .054 31000 , color(black)) ///
	(line eitc_amnt2 eitc_cutoff2, lwidth(thin) lpattern(_-_) lcolor(maroon*.6) yaxis(2) ///
	text(.055 38000  "2 Eligible Children", size(vsmall))) ///
	(pcarrowi .053 38000 .025 35000 , color(black)) ///
	, legend(off /*order(1 "Impact of Out-of-State Friend Exposure at Mean") rows(1)*/) graphregion(color(white)) ///
	xlabel(2500(5000)42500, valuelabels labsize(small)) xtitle("") ytitle(Percentage Points, axis(1)) ///
	ylabel(-.08(.04).12, axis(1)) yscale(range(-4050 6000) axis(2)) ylabel(0(2000)6000, axis(2)) ytitle("EITC Amount ($)", axis(2))
	
cd $output
graph export impact_noneitcearningdist_direct.png, replace


/******************************
Figure A5. Impact on Business Income and Total Income Distribution in ACS
******************************/
cd $data
use cty_eitcnetwork1999_2013, clear

qui reghdfe eitc_rate eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1, de
local mean = r(mean)
loca sd = r(sd)

cd $data
use  hh_selfemp_network05_17, clear

egen st_yr = group(stfips year)
egen st_cz = group(stfips czone)
gen stdcty_eitcexp_fblinkspp = (eitcexp_fblinkspp-`mean')/`sd'

gen tot_ekids0 = tot_numekids == 0
gen tot_ekids1 = tot_numekids == 1
gen tot_ekids2 = tot_numekids == 2
gen tot_ekids3plus = tot_numekids >=3 & tot_numekids ~= .

gen businc_neg5 = f_incbus002017<=-5000
gen businc_0_neg5k = f_incbus002017>-5000 & f_incbus002017<0
gen businc_0 = f_incbus002017 == 0
gen businc_5k_0 = f_incbus002017>0 & f_incbus002017<=5000
gen businc_10k_5 = f_incbus002017>5000 & f_incbus002017<=10000
gen businc_15k_10 = f_incbus002017>10000 & f_incbus002017<=15000
gen businc_20k_15 = f_incbus002017>15000 & f_incbus002017<=20000
gen businc_25k_20 = f_incbus002017>20000 & f_incbus002017<=25000
gen businc_30k_25 = f_incbus002017>25000 & f_incbus002017<=30000
gen businc_35k_30 = f_incbus002017>30000 & f_incbus002017<=35000
gen businc_40k_35 = f_incbus002017>35000 & f_incbus002017<=40000
gen businc_45k_40 = f_incbus002017>40000 & f_incbus002017<=45000
gen businc_over45k = f_incbus002017>45000

for X in any neg5 0_neg5k 0 5k_0 10k_5 15k_10 20k_15 25k_20 30k_25 35k_30 40k_35 45k_40 over45k: replace businc_X = . if f_incbus002017 == .

for X in any g group beta se init_v m_lb m_ub: cap drop X
local groups = "neg5 0_neg5k 0 5k_0 10k_5 15k_10 20k_15 25k_20 30k_25 35k_30 40k_35 45k_40 over45k"
local n : word count `groups'
gen g = _n if _n<=`n'
gen group = ""
gen beta = .
gen se = .
gen init_v = .
gen m_lb = .
gen m_ub = .


//Effect on Levels
forval i = 1/`n'  {
	local outcome : word `i' of `groups'
	reghdfe businc_`outcome' stdcty_eitcexp_fblinkspp $acscontrols lshs somcoll coll advcoll single_mominfam tot_ekids* [aw = czhhwt] , absorb(st_cz st_yr) vce(cluster stfips)
	replace group = "outcome'" if g == `i'
	replace beta = _b[stdcty_eitcexp_fblinkspp] if g == `i'
	replace se = _se[stdcty_eitcexp_fblinkspp] if g == `i'
	scalar edf_r`outcome' = e(df_r)
	qui sum businc_`outcome' [aw = czhhwt] if e(sample) == 1
	replace init_v = r(mean) if g == `i'
	
	replace m_lb = beta-se*invttail(`=scalar(edf_r`outcome')',.025) if g == `i'
	replace m_ub = beta+se*invttail(`=scalar(edf_r`outcome')',.025) if g == `i'
	
	
}
foreach v in beta se m_lb m_ub {
	replace `v' = `v'*100
}

lab def busincgroup 1 "<-5K" 2 "0 to -5K" 3 "None" 4 "0-5K" 5 "5K to 10K" 6 "10K to 15K" 7 "15K to 20K" 8 "20K to 25K" 9 "25K to 30K" 10 "30K to 35K" 11 "35K to 40K" 12 "40K to 45K" 13 "+45K", replace
lab val g busincgroup

twoway (bar beta g, barwidth(1) lcolor(black) fcolor(none) ) ///
	(rcap m_lb m_ub g, lcolor(gray) lwidth(vthin)) ///
	, legend(off) graphregion(color(white)) ///
	xlabel(1(1)13, valuelabels labsize(*.5) angle(45)) xtitle("") ytitle(Percentage Points) ///
	ylabel(-.8(.2).2) title("Business Income (2017$)")


cd $output
graph export impact_busincbin_acs.png, replace

//remove the 0 bin
gen g2 = g
replace g2 = . if g == 3
replace g2 = g2-1 if g>3
lab def busincgroup2 1 "<-5K" 2 "0 to -5K" 3 "0-5K" 4 "5K to 10K" 5 "10K to 15K" 6 "15K to 20K" 7 "20K to 25K" 8 "25K to 30K" 9 "30K to 35K" 10 "35K to 40K" 11 "40K to 45K" 12 "+45K", replace
lab val g2 busincgroup2

twoway (bar beta g2 if g2~=., barwidth(1) lcolor(black) fcolor(none) ) ///
	(rcap m_lb m_ub g2  if g2~=., lcolor(gray) lwidth(vthin)) ///
	, legend(off) graphregion(color(white)) ///
	xlabel(1(1)12, valuelabels labsize(*.5) angle(45)) xtitle("") ytitle(Percentage Points) ///
	ylabel(-.1(.1).2) title("Business Income (2017$)")


cd $output
graph export impact_busincbin_acsno0.png, replace


/******************************
Subfigure Impact on Total Income Distribution in ACS
******************************/
cd $data
use cty_eitcnetwork1999_2013, clear

qui reghdfe eitc_rate eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1, de
local mean = r(mean)
loca sd = r(sd)

cd $data
use  hh_selfemp_network05_17, clear

egen st_yr = group(stfips year)
egen st_cz = group(stfips czone)
gen stdcty_eitcexp_fblinkspp = (eitcexp_fblinkspp-`mean')/`sd'

gen tot_ekids0 = tot_numekids == 0
gen tot_ekids1 = tot_numekids == 1
gen tot_ekids2 = tot_numekids == 2
gen tot_ekids3plus = tot_numekids >=3 & tot_numekids ~= .

gen inc_neg5 = ftotinc2017<=-5000
gen inc_0_neg5k = ftotinc2017>-5000 & ftotinc2017<0
gen inc_0 = ftotinc2017 == 0
gen inc_5k_0 = ftotinc2017>0 & ftotinc2017<=5000
gen inc_10k_5 = ftotinc2017>5000 & ftotinc2017<=10000
gen inc_15k_10 = ftotinc2017>10000 & ftotinc2017<=15000
gen inc_20k_15 = ftotinc2017>15000 & ftotinc2017<=20000
gen inc_25k_20 = ftotinc2017>20000 & ftotinc2017<=25000
gen inc_30k_25 = ftotinc2017>25000 & ftotinc2017<=30000
gen inc_35k_30 = ftotinc2017>30000 & ftotinc2017<=35000
gen inc_40k_35 = ftotinc2017>35000 & ftotinc2017<=40000
gen inc_45k_40 = ftotinc2017>40000 & ftotinc2017<=45000
gen inc_over45k = ftotinc2017>45000

for X in any neg5 0_neg5k 0 5k_0 10k_5 15k_10 20k_15 25k_20 30k_25 35k_30 40k_35 45k_40 over45k: replace inc_X = . if ftotinc2017 == .

for X in any g group beta se init_v m_lb m_ub: cap drop X
local groups = "neg5 0_neg5k 0 5k_0 10k_5 15k_10 20k_15 25k_20 30k_25 35k_30 40k_35 45k_40 over45k"
local n : word count `groups'
gen g = _n if _n<=`n'
gen group = ""
gen beta = .
gen se = .
gen init_v = .
gen m_lb = .
gen m_ub = .


//Effect on Levels
forval i = 1/`n'  {
	local outcome : word `i' of `groups'
	reghdfe inc_`outcome' stdcty_eitcexp_fblinkspp $acscontrols lshs somcoll coll advcoll single_mominfam tot_ekids* [aw = czhhwt] , absorb(st_cz st_yr) vce(cluster stfips)
	replace group = "outcome'" if g == `i'
	replace beta = _b[stdcty_eitcexp_fblinkspp] if g == `i'
	replace se = _se[stdcty_eitcexp_fblinkspp] if g == `i'
	scalar edf_r`outcome' = e(df_r)
	qui sum inc_`outcome' [aw = czhhwt] if e(sample) == 1
	replace init_v = r(mean) if g == `i'
	
	replace m_lb = beta-se*invttail(`=scalar(edf_r`outcome')',.025) if g == `i'
	replace m_ub = beta+se*invttail(`=scalar(edf_r`outcome')',.025) if g == `i'
	
	
}

foreach v in beta se m_lb m_ub {
	replace `v' = `v'*100
}

lab def busincgroup 1 "<-5K" 2 "0 to -5K" 3 "None" 4 "0-5K" 5 "5K to 10K" 6 "10K to 15K" 7 "15K to 20K" 8 "20K to 25K" 9 "25K to 30K" 10 "30K to 35K" 11 "35K to 40K" 12 "40K to 45K" 13 "+45K", replace
lab val g busincgroup

twoway (bar beta g, barwidth(1) lcolor(black) fcolor(none) ) ///
	(rcap m_lb m_ub g, lcolor(gray) lwidth(vthin)) ///
	, legend(off) graphregion(color(white)) ///
	xlabel(1(1)13, valuelabels labsize(*.5) angle(45)) xtitle("") ytitle(Percentage Points) ///
	ylabel(-.8(.2).2) title("Total Family Income (2017$)")


cd $output
graph export impact_totincbin_acs.png, replace

//remove the 0 bin
gen g2 = g
replace g2 = . if g == 3
replace g2 = g2-1 if g>3
lab def busincgroup2 1 "<-5K" 2 "0 to -5K" 3 "0 to 5K" 4 "5K to 10K" 5 "10K to 15K" 6 "15K to 20K" 7 "20K to 25K" 8 "25K to 30K" 9 "30K to 35K" 10 "35K to 40K" 11 "40K to 45K" 12 "+45K", replace
lab val g2 busincgroup2

twoway (bar beta g2 if g2~=., barwidth(1) lcolor(black) fcolor(none) ) ///
	(rcap m_lb m_ub g2  if g2~=., lcolor(gray) lwidth(vthin)) ///
	, legend(off) graphregion(color(white)) ///
	xlabel(1(1)12, valuelabels labsize(*.5) angle(45)) xtitle("") ytitle(Percentage Points) ///
	ylabel(-.8(.2).4) title("Total Family Income (2017$)")


cd $output
graph export impact_totincbin_acsno0.png, replace


/*****************
Figure A6. Pre-Trends for Network Effects on EITC Income Percentages
*****************/
cd $data
use cty_eitcnetwork1999_2013, clear

qui reghdfe eitc_rate eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1, de
local mean = r(mean)
loca sd = r(sd)
cd $data
use events_panel, clear
drop if o_stfips == exp_stfips

gen std_eitcexp_fblinkspp = (eitcexp_fblinkspp-`mean')/`sd'

merge m:1 ctyfips year using cty_eitcnetwork1999_2013,
drop if _m == 1 //This is 2014 and 2015, no data
drop _m 

foreach i in 0 5 10 15 20 25 30 35 40 {
	gen pcteitc_`i' = (eagi`i'/eitc_returns)*100
}

//Create event study
forval yr = 1/4 {
	gen yr_neg`yr' = t_yr == -`yr'
	gen yr_pos`yr' = t_yr == `yr'
}
gen yr_0 = t_yr == 0

forval yr = 1/4 {
	gen fblink_yr_neg`yr' = std_eitcexp_fblinkspp*yr_neg`yr'
	gen fblink_yr_pos`yr' = std_eitcexp_fblinkspp*yr_pos`yr'
}
gen fblink_yr_0 = std_eitcexp_fblinkspp*yr_0

egen event_st_yr = group(eventnum year o_stfips)
egen event_cty = group(eventnum ctyfips)

sort eventnum t_yr o_stfips
drop if o_stfips == exp_stfips //A few counties in Connecticut that have missing stfips

gen eitcnonself_rate = ((eitc_returns-eitc_selfemp)/tot_returns)*100

gen period_t = _n-4 if _n<=8


foreach i in 0 5 10 15 20 25 30 35 40 {
	gen beta_`i' = .
	gen se_`i' = .
reghdfe pcteitc_`i' fblink_yr_neg3 fblink_yr_neg2 fblink_yr_0 fblink_yr_pos1 fblink_yr_pos2 fblink_yr_pos3 fblink_yr_pos4 ///
	$controls  [aw = pop2000] if event_yr <2011 & t_yr>=-3 & t_yr<=4, absorb(event_cty event_st_yr) vce(cluster stfips)
	scalar edf_r`i' = e(df_r)
	forval j = 2/3 {
		replace beta_`i' = _b[fblink_yr_neg`j'] if period_t == -`j'
		replace se_`i' = _se[fblink_yr_neg`j'] if period_t == -`j'
		replace beta_`i' = _b[fblink_yr_pos`j'] if period_t == `j'
		replace se_`i' = _se[fblink_yr_pos`j'] if period_t == `j'
	}
	replace beta_`i' = _b[fblink_yr_pos1] if period_t == 1
	replace se_`i' = _se[fblink_yr_pos1] if period_t == 1
	replace beta_`i' = _b[fblink_yr_pos4] if period_t == 4
	replace se_`i' = _se[fblink_yr_pos4] if period_t == 4
	replace beta_`i' = _b[fblink_yr_0] if period_t == 0
	replace se_`i' = _se[fblink_yr_0] if period_t == 0
	
	replace beta_`i' = 0 if period_t == -1
	replace se_`i' = 0 if period_t == -1	
	gen lb_`i' = beta_`i'-se_`i'*invttail(`=scalar(edf_r`i')',.025)
	gen ub_`i' = beta_`i'+se_`i'*invttail(`=scalar(edf_r`i')',.025)
}
local labels = `" "$0-5K" "$5-10K" "$10-15K" "$15-20K" "$20-25K" "$25-30K" "$30-35K" "$35-40K" "$40K+" "'
local n : word count `labels'
forval i = 1/`n' {
	local p = (`i'-1)*5
	local label: word `i' of `labels'
	cd $output
	twoway (scatter beta_`p' period_t, connect(line) mcolor(navy) lcolor(navy) yline(0, lcolor(black) lwidth(vthin))) ///
		(rcap lb_`p' ub_`p' period_t, lcolor(navy*.6) lwidth(vthin)) ///
		, legend(off)  ylabel(-1(0.5)2) subtitle("`label'", size(vlarge)) ///
		graphregion(color(white)) xtitle(Years Relative to Change) xlabel(-3(1)4) ytitle(As Percent of Total EITC Returns) 
	graph export pct`p'_event.png, replace
}
twoway (scatter beta_0 period_t, connect(line) mcolor(navy*.1) lcolor(navy*.1) yline(0, lcolor(black) lwidth(vthin))) ///
	(scatter beta_5 period_t, connect(line) mcolor(navy*.2) lcolor(navy*.2) ) ///
	(scatter beta_10 period_t, connect(line) mcolor(navy*.3) lcolor(navy*.3) ) ///
	(scatter beta_15 period_t, connect(line) mcolor(navy*.4) lcolor(navy*.4) ) ///
	(scatter beta_20 period_t, connect(line) mcolor(navy*.5) lcolor(navy*.5) ) ///
	(scatter beta_25 period_t, connect(line) mcolor(navy*.6) lcolor(navy*.6) ) ///
	(scatter beta_30 period_t, connect(line) mcolor(navy*.7) lcolor(navy*.7) ) ///
	(scatter beta_35 period_t, connect(line) mcolor(navy*.8) lcolor(navy*.8) ) ///
	(scatter beta_40 period_t, connect(line) mcolor(navy*.9) lcolor(navy*.9) ) ///
	, legend(order(1 "$0-5K" 2 "$5-10K" 3 "$10-15K" 4 "$15-20K" 5 "$20-25K" 6 "$25-30K" 7 "$30-35K" 8 "$35-40K" 9 "$40+K") rows(2) symxsize(6) size(small)) ///
	graphregion(color(white)) xtitle(Years Relative to Change) xlabel(-3(1)4) ytitle(As Percent of Total EITC Returns) 

/******************************
Appendix Figure A7. Placebo Impact of Out-of-Friend Exposure on Non-EITC income distribution
******************************/
cd $data
use cty_eitcnetwork1999_2013, clear
foreach i in 0 5 10 15 20 25 30 35 40 {
	gen pcttot_`i' = ((tagi`i'-eagi`i')/(tot_returns-eitc_returns))*100
}

reghdfe eitc_rate eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1, de
gen std_eitcexp_fblinkspp = (eitcexp_fblinkspp-r(mean))/r(sd)

egen rowtotal = rowtotal(pcttot_*)

gen cutoff = _n*5-5 if _n<=9
gen beta = .
gen se = .
gen init_v = .
gen pre_lb = .
gen pre_ub = .
gen m_lb = .
gen m_ub = .

//Effect on Levels
foreach outcome in 0 5 10 15 20 25 30 35 40 {
	reghdfe pcttot_`outcome' std_eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
	replace beta = _b[std_eitcexp_fblinkspp] if cutoff == `outcome'
	replace se = _se[std_eitcexp_fblinkspp] if cutoff == `outcome'
	scalar edf_r`outcome' = e(df_r)
	qui sum pcttot_`outcome' [aw = pop2000] if e(sample) == 1
	replace init_v = r(mean) if cutoff == `outcome'
}

egen t = sum(init_v)

gen pre_val = init_v+beta

foreach outcome in 0 5 10 15 20 25 30 35 40 {

	replace m_lb = beta-se*invttail(`=scalar(edf_r`outcome')',.025) if cutoff == `outcome'
	replace m_ub = beta+se*invttail(`=scalar(edf_r`outcome')',.025) if cutoff == `outcome'
	
	replace pre_lb = pre_val-se*invttail(`=scalar(edf_r`outcome')',.025) if cutoff == `outcome'
	replace pre_ub = pre_val+se*invttail(`=scalar(edf_r`outcome')',.025) if cutoff == `outcome'
}

//I want the cutoff to be at the lower end of the bar and in thousands of dollars
gen cutoff_mod = cutoff*1000+2500
lab def cutoffs 2500 "Under $5k" 7500 "$5-10K" 12500 "$10-15K" 17500 "$15-20K" 22500 "$20-25K" 27500 "$25-30K" 32500 "$30-35K" 37500 "$35-40K" 42500 "$40-50K", replace
lab val cutoff_mod cutoffs


//I also want to plot the EITC schedule
gen eitc_cutoff2 = .
replace eitc_cutoff2 = 0 if _n == 1
replace eitc_cutoff2 = 12590 if _n == 2
replace eitc_cutoff2 = 16450 if _n == 3
replace eitc_cutoff2 = 40363 if _n == 4

gen eitc_amnt2 = .
replace eitc_amnt2 = 0 if _n == 1
replace eitc_amnt2 = 5036 if _n == 2
replace eitc_amnt2 = 5036 if _n == 3
replace eitc_amnt2 = 0 if _n == 4

gen eitc_cutoff3 = .
replace eitc_cutoff3 = 0 if _n == 1
replace eitc_cutoff3 = 12590 if _n == 2
replace eitc_cutoff3 = 16450 if _n == 3
replace eitc_cutoff3 = 43352 if _n == 4

gen eitc_amnt3 = .
replace eitc_amnt3 = 0 if _n == 1
replace eitc_amnt3 = 5666 if _n == 2
replace eitc_amnt3 = 5666 if _n == 3
replace eitc_amnt3 = 0 if _n == 4


lab val eitc_cutoff2 cutoffs
lab val eitc_cutoff3 cutoffs

twoway (bar beta cutoff_mod, barwidth(5000) lcolor(black) fcolor(none) yaxis(1)) ///
	(rcap m_ub m_lb cutoff_mod, lcolor(black) lwidth(vthin) yaxis(1)) ///
	(line eitc_amnt3 eitc_cutoff3, lwidth(thin) lpattern(__) lcolor(navy*.6) yaxis(2) ///
	text(.55 33300  "3+ Eligible Children", size(vsmall))) ///
	(pcarrowi .52 33300 .34 31000 , color(black)) ///
	(line eitc_amnt2 eitc_cutoff2, lwidth(thin) lpattern(_-_) lcolor(maroon*.6) yaxis(2) ///
	text(.45 38000  "2 Eligible Children", size(vsmall))) ///
	(pcarrowi .43 38000 .15 35000 , color(black)) ///
	, legend(off /*order(1 "Impact of Out-of-State Friend Exposure at Mean") rows(1)*/) graphregion(color(white)) ///
	xlabel(2500(5000)42500, valuelabels labsize(small)) xtitle("") ytitle(Percentage Points, axis(1)) ///
	ylabel(-.5(.5)1, axis(1)) yscale(range(-4000 8000) axis(2)) ylabel(0(2000)6000, axis(2)) ytitle("EITC Amount ($)", axis(2))
	
cd $output
graph export impact_noneitcearningdist.png, replace

/***********************
Appendix Figure A8. Map of Interstate Counties
***********************/
use $data\shapefiles\freewaycoord.dta, clear

drop if inlist(_ID,71,72,80,82,689)
drop if inlist(_ID,17,18,19,20,49,50, 51,54,64)
cd $data
cd shapefiles
save road_lines, replace

cd $data
use cty_eitcnetwork1999_2013, clear

keep if year == 2013
keep ctyfips year pctany_steitc steitc_pct eitcexp_fblinkspp
merge 1:1 ctyfips using cty_interstates
drop if _m == 2
gen any_interstate = _m == 3
drop _m

tempfile cty_rates

save `cty_rates'

cd $data
cd shapefiles

use countydb, clear
destring GEOID, gen(ctyfips)
merge 1:1 ctyfips using `cty_rates'

replace any_interstate = 0 if ctyfips == 46102
cd $data
cd shapefiles
spmap any_interstate using countycoord if ~inlist(STATEFP,"02","15","60","66","69","72","78"), id(id) ///
	line(data(road_lines)) fcolor(gray*.1 navy*.6) ocolor(gray*.4 navy*.4) ///
	legend(off) saving(interstate_map, replace)
	
cd $output
graph export interstate_map.png, replace


/******************************
Appendix Figure A9. How well do Google Searches track Tax Cycle?
******************************/
//DMA Level
cd $data
use dma_eitcgoogletrend_network04_17, clear

collapse (mean) eic eitc taxcredit refund earnedincomecredit earnedincometaxcredit selfemployed schedule*, by(year month)

gen year_m = year+(month-1)/12

twoway (line eic year_m, lcolor(navy)) ///
	(line eitc year_m, lcolor(maroon*.5) ) ///
	(line earnedincomecredit year_m, lcolor(navy*.8) lpattern(__) ) ///
	(line earnedincometaxcredit year_m, lcolor(maroon*.7) lpattern(__) ) ///
	, legend(order(1 "eic" 2 "eitc" 3 "earned income credit" 4 "earned income tax credit") rows(1) symxsize(6) size(small)) ///
	graphregion(color(white)) xtitle(Date) xlabel(2004(2)2018) ytitle("Google Search Intensity Score")

cd $output
graph export dma_eitc_terms.png, replace

twoway (line selfemployed year_m, lcolor(navy)) ///
	(line schedulec year_m, lcolor(maroon*.5) ) ///
	, legend(order(1 "self employed" 2 "schedule c") rows(1) symxsize(6) size(small)) ///
	graphregion(color(white)) xtitle(Date) xlabel(2004(2)2018) ytitle("Google Search Intensity Score")

cd $output
graph export dma_selfemp_terms.png, replace



/***********************************
***********APPENDIX TABLES**********
***********************************/
/******************************
Appendix Tabe A1. Direct Impact of State EITC Expansions on EITC Claiming Behavior, Generalized Fixed Effects
******************************/
cd $data
use cty_eitcnetwork1999_2013, clear

gen eitcnonself_rate = ((eitc_returns-eitc_selfemp)/tot_returns)*100
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	gen ln`outcome' = ln(`outcome')
}
replace eitc_pct = eitc_pct/10

gen haveeitc = eitc_pct >0
gen pct_eitcrange = ((tagi0+tagi5+tagi10+tagi15+tagi20+tagi25+tagi30+tagi35)/tot_returns)
gen bunch515 = ((eagi5+eagi10)/eitc_returns)
bys ctyfips: egen bunch515_2000 = max(bunch515*(year == 2000))

//Effect on Levels
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	reghdfe `outcome' haveeitc $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips year) vce(cluster stfips)
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	qui sum `outcome' [aw = pop2000] if e(sample) == 1
	scalar dmean_`outcome' = r(mean)
	qui sum haveeitc [aw = pop2000] if e(sample) == 1
	scalar imean_`outcome' = r(mean)
}	

cd $output
cap file close regout
file open regout using tab_directstate_twowayfe.tex, write replace
//Header
file write regout "\begin{tabular}{lccccc}" _n
file write regout "\toprule" _n
file write regout "\toprule" _n
file write regout "Generalized Fixed Effects & \multicolumn{3}{c}{Percent of Returns with EITC} &  \\" _n
file write regout "\cmidrule{2-4}" _n
file write regout "Panel A. Levels & & & & Percent of EITC \\" _n
file write regout "&  Any EITC & No Self Employment & Self Employment & Returns with Self Emp. & Average EITC \\" _n
file write regout "&  & (No Schedule C, E, or F) & (Schedule C, E, or F) &  (Schedule C, E, or F) & Amount \\" _n
file write regout "& (1) & (2) & (3) & (4) & (5) \\" _n
file write regout "\midrule" _n
file write regout " \\" _n
//Table
file write regout "Any State EITC" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %7.2f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate  ave_eitc {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
file write regout "\\" _n
//Statistics
file write regout "Dependent Mean" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %5.1f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}  
file write regout "\\" _n

//Effect on Logs
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	reghdfe ln`outcome' haveeitc $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips year) vce(cluster stfips)
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	qui sum `outcome' [aw = pop2000] if e(sample) == 1
	scalar dmean_`outcome' = r(mean)
	qui sum haveeitc [aw = pop2000] if e(sample) == 1
	scalar imean_`outcome' = r(mean)
}	

cd $output
file write regout "\midrule" _n
file write regout "Generalized Fixed Effects & \multicolumn{3}{c}{Log Percent of Returns with EITC} &  \\" _n
file write regout "\cmidrule{2-4}" _n
file write regout "Panel B. Logs & & & & Log Percent of EITC & Log \\" _n
file write regout "&  Any EITC & No Self Employment & Self Employment & Returns with Self Emp. & Average EITC  \\" _n
file write regout "&  & (No Schedule C, E, or F) & (Schedule C, E, or F) &  (Schedule C, E, or F) & Amount \\" _n
file write regout "& (1) & (2) & (3) & (4) & (5) \\" _n
file write regout "\midrule" _n
file write regout " \\" _n
//Table
file write regout "Any State EITC" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %7.2f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
file write regout "& & & &  \\" _n
//Statistics
file write regout "Dependent Mean" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %5.1f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}  
file write regout "\\" _n

file write regout "\\" _n
file write regout "\bottomrule" _n
file write regout "\bottomrule" _n
file write regout "\end{tabular}"
file close regout

/*****************************
Appendix Table A2. Alternative Measures of Direct Effects (Under 40K and Bunching)
*****************************/
cd $data
use cty_eitcnetwork1999_2013, clear

gen eitcnonself_rate = ((eitc_returns-eitc_selfemp)/tot_returns)*100
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	gen ln`outcome' = ln(`outcome')
}
replace eitc_pct = eitc_pct/10

gen haveeitc = eitc_pct >0
gen pct_eitcrange = ((tagi0+tagi5+tagi10+tagi15+tagi20+tagi25+tagi30+tagi35)/tot_returns)*100
gen bunch515 = ((eagi5+eagi10)/eitc_returns)*100
bys ctyfips: egen bunch515_2000 = max(bunch515*(year == 2000))

gen t = pct_eitcrange if year == 2000
bys ctyfips: egen pct_eitcrange2000 = max(t)
gen haveeitc_range2000 = haveeitc*pct_eitcrange2000
gen haveeitc_bunch515 = haveeitc*bunch515_2000


//Percent in Range
//Effect on Levels
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	reghdfe `outcome' haveeitc_range2000 $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	qui sum `outcome' [aw = pop2000] if e(sample) == 1
	scalar dmean_`outcome' = r(mean)
	qui sum pct_eitcrange2000 [aw = pop2000] if e(sample) == 1
	scalar imean_`outcome' = r(mean)
}	
cd $output
cap file close regout
file open regout using tab_directstate_alternate.tex, write replace
//Header
file write regout "\begin{tabular}{lccccc}" _n
file write regout "\toprule" _n
file write regout "\toprule" _n
file write regout "Percent in EITC Range& \multicolumn{3}{c}{Percent of Returns with EITC} &  \\" _n
file write regout "\cmidrule{2-4}" _n
file write regout "Panel A. & & & & Percent of EITC \\" _n
file write regout "&  Any EITC & No Self Employment & Self Employment & Returns with Self Emp. & Average EITC \\" _n
file write regout "&  & (No Schedule C, E, or F) & (Schedule C, E, or F) &  (Schedule C, E, or F) & Amount \\" _n
file write regout "& (1) & (2) & (3) & (4) & (5) \\" _n
file write regout "\midrule" _n
file write regout "\\" _n
//Table
file write regout "Any State EITC*Percent HH" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %7.2f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "\hspace{3mm}in 2000 with Income Below 40K" _n
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
file write regout "& & & &  \\" _n
//Statistics
file write regout "Dependent Mean" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %5.1f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}  
file write regout "\\" _n
//Effect of bunching
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	reghdfe `outcome' haveeitc_bunch515 $controls [aw = pop2000] if pop2000 ~= ., absorb(ctyfips st_yr) vce(cluster stfips)
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	qui sum `outcome' [aw = pop2000] if e(sample) == 1
	scalar dmean_`outcome' = r(mean)
	qui sum bunch515_2000 [aw = pop2000] if e(sample) == 1
	scalar imean_`outcome' = r(mean)
}	
file write regout "\midrule" _n
file write regout "Bunching Measure & \multicolumn{3}{c}{Percent of Returns with EITC} &  \\" _n
file write regout "\cmidrule{2-4}" _n
file write regout "Panel B. & & & & Percent of EITC \\" _n
file write regout "&  Any EITC & No Self Employment & Self Employment & Returns with Self Emp. & Average EITC  \\" _n
file write regout "&  & (No Schedule C, E, or F) & (Schedule C, E, or F) &  (Schedule C, E, or F) & Amount \\" _n
file write regout "& (1) & (2) & (3) & (4) & (5) \\" _n
file write regout "\midrule" _n
file write regout "\\" _n
//Table
file write regout "Any State EITC*Percent EITC HH" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %7.2f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "\hspace{3mm}in 2000 with Income 5K-15K" _n
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
file write regout "& & & &  \\" _n
//Statistics
file write regout "Dependent Mean" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %5.1f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}  
file write regout "\\" _n
file write regout "\bottomrule" _n
file write regout "\bottomrule" _n
file write regout "\end{tabular}"
file close regout

/**********************
Appendix Table A3. Impact of State EITC on Income Distribution, Alternative Measures (Under 40K and Bunching)
**********************/
cd $data
use cty_eitcnetwork1999_2013, clear

gen eitcnonself_rate = ((eitc_returns-eitc_selfemp)/tot_returns)*100
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	gen ln`outcome' = ln(`outcome')
}
replace eitc_pct = eitc_pct/10

gen haveeitc = eitc_pct >0
gen pct_eitcrange = ((tagi0+tagi5+tagi10+tagi15+tagi20+tagi25+tagi30+tagi35)/tot_returns)*100

gen bunch515 = ((eagi5+eagi10)/eitc_returns)*100
bys ctyfips: egen bunch515_2000 = max(bunch515*(year == 2000))

gen t = pct_eitcrange if year == 2000
bys ctyfips: egen pct_eitcrange2000 = max(t)
gen haveeitc_range2000 = haveeitc*pct_eitcrange2000
gen haveeitc_bunch515 = haveeitc*bunch515_2000


//Now look at distribution
foreach i in 0 5 10 15 20 25 30 35 40 {
	gen pcteitc_`i' = (eagi`i'/eitc_returns)*100
}	
//Effect of in EITC range
foreach outcome in pcteitc_0 pcteitc_5 pcteitc_10 pcteitc_15 pcteitc_20 pcteitc_25 pcteitc_30 pcteitc_35 pcteitc_40 {
	reghdfe `outcome' haveeitc_range2000  $controls [aw = pop2000] if pop2000 ~= . , absorb(ctyfips st_yr) vce(cluster ctyfips)
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	qui sum `outcome' [aw = pop2000] if e(sample) == 1
	scalar dmean_`outcome' = r(mean)
	qui sum pct_eitcrange2000 [aw = pop2000] if e(sample) == 1
	scalar imean_`outcome' = r(mean)
}	

cd $output
cap file close regout
file open regout using tab_directstateeitc_dist_alt.tex, write replace
//Header
file write regout "\begin{tabular}{lccccccccc}" _n
file write regout "\toprule" _n
file write regout "\toprule" _n
file write regout "& \multicolumn{9}{c}{Percent of EITC Returns by Income Bin}  \\" _n
file write regout "\cmidrule{2-10}" _n
file write regout `" & Under \\$5K & \\$5-10K & \\$10-15K & \\$15-20K & \\$20-25K & \\$25-30K & \\$30-35K & \\$35-40K & Over \\$40K \\"' _n
file write regout "& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) & (9) \\" _n
file write regout "\midrule" _n
file write regout " \\" _n
//Table
file write regout "Any State EITC*Percent HH" 
foreach outcome in pcteitc_0 pcteitc_5 pcteitc_10 pcteitc_15 pcteitc_20 pcteitc_25 pcteitc_30 pcteitc_35 pcteitc_40 {
	file write regout "&" %7.2f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "\hspace{3mm}in 2000 with Income Below 40K" _n
foreach outcome in pcteitc_0 pcteitc_5 pcteitc_10 pcteitc_15 pcteitc_20 pcteitc_25 pcteitc_30 pcteitc_35 pcteitc_40 {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
file write regout " \\" _n
//Statistics
file write regout "Dependent Mean" 
foreach outcome in pcteitc_0 pcteitc_5 pcteitc_10 pcteitc_15 pcteitc_20 pcteitc_25 pcteitc_30 pcteitc_35 pcteitc_40 {
	file write regout "&" %5.1f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in pcteitc_0 pcteitc_5 pcteitc_10 pcteitc_15 pcteitc_20 pcteitc_25 pcteitc_30 pcteitc_35 pcteitc_40 {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}  
file write regout "\\" _n
//Effect on Levels
foreach outcome in pcteitc_0 pcteitc_5 pcteitc_10 pcteitc_15 pcteitc_20 pcteitc_25 pcteitc_30 pcteitc_35 pcteitc_40 {
	reghdfe `outcome' haveeitc_bunch515  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	qui sum `outcome' [aw = pop2000] if e(sample) == 1
	scalar dmean_`outcome' = r(mean)
	qui sum bunch515_2000 [aw = pop2000] if e(sample) == 1
	scalar imean_`outcome' = r(mean)
}

//Header
file write regout "\midrule" _n
file write regout "& \multicolumn{9}{c}{Percent of EITC Returns by Income Bin}  \\" _n
file write regout "\cmidrule{2-10}" _n
file write regout `"& Under \\$5K & \\$5-10K & \\$10-15K & \\$15-20K & \\$20-25K & \\$25-30K & \\$30-35K & \\$35-40K & Over \\$40K \\"' _n
file write regout "& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) & (9) \\" _n
file write regout "\midrule" _n
file write regout " \\" _n
//Table
file write regout "Any State EITC*Share EITC HH" 
foreach outcome in pcteitc_0 pcteitc_5 pcteitc_10 pcteitc_15 pcteitc_20 pcteitc_25 pcteitc_30 pcteitc_35 pcteitc_40 {
	file write regout "&" %7.2f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\ \hspace{3mm}in 2000 with Income 5K-15K" _n
foreach outcome in pcteitc_0 pcteitc_5 pcteitc_10 pcteitc_15 pcteitc_20 pcteitc_25 pcteitc_30 pcteitc_35 pcteitc_40 {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
file write regout "\\" _n
//Statistics
file write regout "Dependent Mean" 
foreach outcome in pcteitc_0 pcteitc_5 pcteitc_10 pcteitc_15 pcteitc_20 pcteitc_25 pcteitc_30 pcteitc_35 pcteitc_40 {
	file write regout "&" %5.1f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in pcteitc_0 pcteitc_5 pcteitc_10 pcteitc_15 pcteitc_20 pcteitc_25 pcteitc_30 pcteitc_35 pcteitc_40 {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}  
file write regout "\\" _n
file write regout "\bottomrule" _n
file write regout "\bottomrule" _n
file write regout "\end{tabular}"
file close regout

/*****************************
Appendix Table A4. Heterogeneity in ACS
*****************************/
//Estimated above so you only read in the ACS file once

/**********************
Appendix Table A5. Relationship Between Out-of-State Friend Exposure to State EITC and Self Filing
**********************/
cd $data
use cty_eitcnetwork1999_2013, clear

gen eitcselffile_rate = (eitc_selffile/tot_returns)*100
gen selfileamongeitc_rate = (eitc_selffile/eitc_returns)*100

foreach outcome in eitcselffile_rate selfileamongeitc_rate {
	gen ln`outcome' = ln(`outcome')
}
reghdfe eitc_rate eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
	qui sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1, de
gen std_eitcexp_fblinkspp = (eitcexp_fblinkspp-r(mean))/r(sd)

//Effect on Levels
foreach outcome in eitcselffile_rate lneitcselffile_rate {
	reghdfe `outcome' std_eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	qui sum `outcome' [aw = pop2000] if e(sample) == 1
	scalar dmean_`outcome' = r(mean)
	qui sum std_eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1
	scalar imean_`outcome' = r(mean)
}	

cd $output
cap file close regout
file open regout using tab_selffile.tex, write replace
//Header
file write regout "\begin{tabular}{lcc}" _n
file write regout "\toprule" _n
file write regout "\toprule" _n
file write regout "& \multicolumn{2}{c}{Percent of Returns with EITC and Self Filed} \\" _n
file write regout "& Level & Logs \\"
file write regout "& (1) & (2) \\" _n
file write regout "\midrule" _n
file write regout "& & \\" _n
//Table
file write regout "Out-of-State Friends per person" 
foreach outcome in eitcselffile_rate lneitcselffile_rate {
	file write regout "&" %7.2f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "Exposed to State EITC (Standardized)" 
foreach outcome in eitcselffile_rate lneitcselffile_rate {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
file write regout "& & \\" _n
//Statistics
file write regout "Dependent Mean" 
foreach outcome in eitcselffile_rate lneitcselffile_rate {
	file write regout "&" %5.1f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in eitcselffile_rate lneitcselffile_rate {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}  
file write regout "\\" _n
file write regout "\bottomrule" _n
file write regout "\bottomrule" _n
file write regout "\end{tabular}"
file close regout

/******************************
Appendix Table A6. Robustness of Social Network Effects 
******************************/
cd $data
use master_cty_distance_long, clear

/*keep if inlist(stfips,6,8,9,10,11,17,18,19,20,22,23,24,25,26,31,33,34,35,36,37,39,40,41,44,50,51,55)*/ //all EITC states
keep if inlist(stfips,8,9,10,11,17,18,22,23,26,31,33,34,35,37,39,40,44,51) //States that introduced EITC from 1999 to 2013
gen border_cty = away_from_ == 1  //These are counties that border one of the expansion states
collapse (max) border_cty, by(ctyfips)

cd $data
merge 1:m ctyfips using cty_eitcnetwork1999_2013
drop if _m == 1 
drop _m

//Merge on county population
merge 1:1 ctyfips year using seer_pop90_18, keepusing(totalpop)
drop if _m == 2
drop _m
replace totalpop = totalpop/10000
//Merge on DMA
cd $data
merge m:1 ctyfips using cty_dma_xwalk2014
drop if _m == 2 //VA cities and Alaska
drop _m


egen dma_yr = group(dma_id year)

foreach e in lshs hs scoll coll {
	gen popshare`e' = pop_`e'2000 if year == 2000
	replace popshare`e' = pop_`e'2013 if year == 2013
}

foreach var in popshare_female popshare_nhwhite popshare_nhblack popshare_hisp popshare_nhother popsharelshs popsharehs popsharescoll popsharecoll urate ave_jobearn {
	bys ctyfips: egen yr2000_`var' = max(`var'*(year == 2000))
}

gen eitcnonself_rate = ((eitc_returns-eitc_selfemp)/tot_returns)*100
gen lneitcself_rate = ln(eitcself_rate)
gen lneitcnonself_rate = ln(eitcnonself_rate)

qui reghdfe ave_eitc eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= ., absorb(ctyfips st_yr) vce(cluster ctyfips)
qui sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1, de
local p99 = r(p99)
gen std_eitcexp_fblinkspp = (eitcexp_fblinkspp-r(mean))/r(sd)


local config = `" "eitcself_rate std_eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= ., absorb(ctyfips st_yr) vce(cluster stfips)" "eitcself_rate std_eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & eitcexp_fblinkspp<`p99', absorb(ctyfips st_yr) vce(cluster stfips)" "eitcself_rate std_eitcexp_fblinkspp [aw = pop2000] if pop2000 ~= ., absorb(ctyfips st_yr) vce(cluster stfips)" "eitcself_rate std_eitcexp_fblinkspp $controls [aw = pop2000] if pop2000 ~= ., absorb(ctyfips st_yr i.ctyfips#c.year) vce(cluster stfips)" "eitcself_rate std_eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= ., absorb(ctyfips year) vce(cluster stfips)" "eitcself_rate std_eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= ., absorb(ctyfips st_yr dma_yr) vce(cluster stfips)" "eitcself_rate std_eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & border_cty ~=1, absorb(ctyfips st_yr) vce(cluster stfips)" "lneitcself_rate std_eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= ., absorb(ctyfips st_yr) vce(cluster stfips)" "eitcself_rate std_eitcexp_fblinkspp  $controls if pop2000 ~= ., absorb(ctyfips st_yr) vce(cluster stfips)" "lneitcself_rate std_eitcexp_fblinkspp  $controls if pop2000 ~= ., absorb(ctyfips st_yr) vce(cluster stfips)" "eitcself_rate std_eitcexp_fblinkspp totalpop $controls [aw = pop2000] if pop2000 ~= ., absorb(ctyfips st_yr) vce(cluster stfips)" "'

forval outcome = 1/11 {
	local regspec : word `outcome' of `config'
	reghdfe `regspec'
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	scalar dmean_`outcome' = r(mean)
	qui sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1
	scalar imean_`outcome' = r(mean)
}	
cd $output
cap file close regout
file open regout using tab_robust.tex, write replace
//Header
file write regout "\begin{tabular}{lccccccccccc}" _n
file write regout "\toprule" _n
file write regout "\toprule" _n
file write regout "& & Trim & No & County & Year & DMA by & Exclude & Outcome in & Un- & Un-weighted, & Control for \\ "_n
file write regout "& Baseline & top 1\% & Controls & Trends & F.E. & Year F.E. & Border Cnty. & Logs & weighted & in Logs & Population \\ "_n
file write regout "& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) & (9) & (10) & (11) \\" _n
file write regout "\midrule" _n
file write regout "& \multicolumn{11}{c}{Percent of Returns with EITC, Self Employment} \\ " _n
file write regout " \\" _n
//Table
file write regout "Out-of-State Friends " 
forval outcome = 1/11 {
	file write regout "&" %7.2f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "per person Exposed to" 
forval outcome = 1/11 {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
file write regout "State EITC (Std.)  \\" _n
//Statistics
file write regout "Observations" 
forval outcome = 1/11 {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}  
file write regout "\\" _n

local config = `" "eitcnonself_rate std_eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= ., absorb(ctyfips st_yr) vce(cluster stfips)" "eitcnonself_rate std_eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & eitcexp_fblinkspp<`p99', absorb(ctyfips st_yr) vce(cluster stfips)" "eitcnonself_rate std_eitcexp_fblinkspp [aw = pop2000] if pop2000 ~= ., absorb(ctyfips st_yr) vce(cluster stfips)" "eitcnonself_rate std_eitcexp_fblinkspp $controls [aw = pop2000] if pop2000 ~= ., absorb(ctyfips st_yr i.ctyfips#c.year) vce(cluster stfips)" "eitcnonself_rate std_eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= ., absorb(ctyfips year) vce(cluster stfips)" "eitcnonself_rate std_eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= ., absorb(ctyfips st_yr dma_yr) vce(cluster stfips)" "eitcnonself_rate std_eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & border_cty ~=1, absorb(ctyfips st_yr) vce(cluster stfips)" "lneitcnonself_rate std_eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= ., absorb(ctyfips st_yr) vce(cluster stfips)" "eitcnonself_rate std_eitcexp_fblinkspp  $controls if pop2000 ~= ., absorb(ctyfips st_yr) vce(cluster stfips)" "lneitcnonself_rate std_eitcexp_fblinkspp  $controls if pop2000 ~= ., absorb(ctyfips st_yr) vce(cluster stfips)" "eitcnonself_rate std_eitcexp_fblinkspp totalpop $controls [aw = pop2000] if pop2000 ~= ., absorb(ctyfips st_yr) vce(cluster stfips)" "'

forval outcome = 1/11 {
	local regspec : word `outcome' of `config'
	reghdfe `regspec'
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	scalar dmean_`outcome' = r(mean)
	qui sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1
	scalar imean_`outcome' = r(mean)
}	
file write regout "\midrule" _n
file write regout "& \multicolumn{11}{c}{Percent of Returns with EITC, No Self Employment} \\ " _n
file write regout "\\" _n

//Table
file write regout "Out-of-State Friends" 
forval outcome = 1/11 {
	file write regout "&" %7.2f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "per person Exposed to" 
forval outcome = 1/11 {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
file write regout " State EITC (Std.)  \\" _n
//Statistics
file write regout "Observations" 
forval outcome = 1/11 {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}  
file write regout "\\" _n
file write regout "\bottomrule" _n
file write regout "\bottomrule" _n
file write regout "\end{tabular}"
file close regout

/******************************
Appendix Table A7. Role of Migration Flows
******************************/
cd $data
use cty_eitcnetwork1999_2013, clear
gen eitcnonself_rate = ((eitc_returns-eitc_selfemp)/tot_returns)*100

merge 1:1 ctyfips year using migration_rates
drop if _m == 2 //years before and after sample
drop _m
drop exemptions returns

replace outstate_migrate = 0 if outstate_migrate == .
replace outeitcstate_migrate = 0 if outeitcstate_migrate == .
replace outeitcstate3_migrate = 0 if outeitcstate3_migrate == .

reghdfe eitc_rate eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
	qui sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1, de
gen std_eitcexp_fblinkspp = (eitcexp_fblinkspp-r(mean))/r(sd)
	
	
//Effect on Levels
foreach outcome in outeitcstate_migrate {
	reghdfe `outcome' std_eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	qui sum `outcome' [aw = pop2000] if e(sample) == 1
	scalar dmean_`outcome' = r(mean)
	qui sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1
	scalar imean_`outcome' = r(mean)
}		
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate {
	reghdfe `outcome' std_eitcexp_fblinkspp  $controls outeitcstate_migrate [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	qui sum `outcome' [aw = pop2000] if e(sample) == 1
	scalar dmean_`outcome' = r(mean)
	qui sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1
	scalar imean_`outcome' = r(mean)
}

cd $output
cap file close regout
file open regout using tab_migration.tex, write replace
//Header
file write regout "\begin{tabular}{lcccc}" _n
file write regout "\toprule" _n
file write regout "\toprule" _n
file write regout "& &  \multicolumn{3}{c}{Percent of Returns with EITC} \\" _n
file write regout "\cmidrule{3-5}"
file write regout "& Individual In Migration& Any EITC & No Self Employment & Self Employment  \\"
file write regout "& Rate - EITC States & & (No Schedule C, E, or F) & (Schedule C, E, or F) \\"
file write regout "& (1) & (2) & (3) & (4) \\" _n
file write regout "\midrule" _n
file write regout "& & & & \\" _n
//Table
file write regout "Out-of-State Friends per person" 
foreach outcome in outeitcstate_migrate eitc_rate eitcnonself_rate eitcself_rate {
	file write regout "&" %7.2f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "Exposed to State EITC (Standardized)" 
foreach outcome in outeitcstate_migrate eitc_rate eitcnonself_rate eitcself_rate {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
file write regout "& & \\" _n
//Statistics
file write regout "Dependent Mean" 
foreach outcome in outeitcstate_migrate eitc_rate eitcnonself_rate eitcself_rate {
	file write regout "&" %5.1f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in outeitcstate_migrate eitc_rate eitcnonself_rate eitcself_rate {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}  
file write regout "\\" _n
file write regout "\bottomrule" _n
file write regout "\bottomrule" _n
file write regout "\end{tabular}"
file close regout

/************************
Appendix Table A8. Impacts on Non-EITC outcomes
************************/
cd $data
use cty_eitcnetwork1999_2013, clear

gen eitcnonself_rate = ((eitc_returns-eitc_selfemp)/tot_returns)*100
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	gen ln`outcome' = ln(`outcome')
}
reghdfe eitc_rate eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
sum eitcexp_fblinkspp [aw = pop2000] if  e(sample) == 1, de
gen std_eitcexp_fblinkspp = (eitcexp_fblinkspp-r(mean))/r(sd)
	
gen noneitcself_totrate = ((tot_selfemp-eitc_selfemp)/(tot_returns))*100
gen noneitcself_rate = ((tot_selfemp-eitc_selfemp)/(tot_returns-eitc_returns))*100
gen lntot_returns = ln(tot_returns) //we only have the total number of returns, not a rate


//Effect on Levels
foreach outcome in lntot_returns noneitcself_totrate noneitcself_rate {
	reghdfe `outcome' std_eitcexp_fblinkspp $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	qui sum `outcome' [aw = pop2000] if e(sample) == 1
	scalar dmean_`outcome' = r(mean)
	qui sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1
	scalar imean_`outcome' = r(mean)
}	

cd $output
cap file close regout
file open regout using tab_noneitcoutcomes.tex, write replace
//Header
file write regout "\begin{tabular}{lccc}" _n
file write regout "\toprule" _n
file write regout "\toprule" _n
file write regout "& & Percent of Total Returns & Percent of Non-EITC Returns \\" _n
file write regout "& Log Total & with Self Employment & with Self Employment \\" _n
file write regout "& Returns & and No EITC & No EITC \\" _n
file write regout "& (1) & (2)  & (3) \\" _n
file write regout "\midrule" _n
file write regout "\\" _n
//Table
file write regout "Out-of-State Friends per person" 
foreach outcome in lntot_returns noneitcself_totrate noneitcself_rate {
	file write regout "&" %7.2f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "Exposed to State EITC (Standardized)" 
foreach outcome in lntot_returns noneitcself_totrate noneitcself_rate {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
file write regout " \\" _n
//Statistics
file write regout "Dependent Mean" 
foreach outcome in lntot_returns noneitcself_totrate noneitcself_rate {
	file write regout "&" %5.1f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in lntot_returns noneitcself_totrate noneitcself_rate {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}  
file write regout "\\" _n
file write regout "\bottomrule" _n
file write regout "\bottomrule" _n
file write regout "\end{tabular}"
file close regout


/******************************
Appendix Table A9. Impact of Social Network Exposure on EITC Claiming Behavior Exploiting Expansion County Variation in 1999 EITC Claiming Rate
******************************/
cd $data
use cty_eitcnetwork1999_2013, clear

gen eitcnonself_rate = ((eitc_returns-eitc_selfemp)/tot_returns)*100
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	gen ln`outcome' = ln(`outcome')
}
reghdfe eitc_rate eitcexp_fblinkseitcratepp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
sum eitcexp_fblinkseitcratepp [aw = pop2000] if  e(sample) == 1, de
gen std_eitcexp_fblinkseitcratepp = (eitcexp_fblinkseitcratepp-r(mean))/r(sd)
	
//Effect on Levels
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	reghdfe `outcome' std_eitcexp_fblinkseitcratepp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	qui sum `outcome' [aw = pop2000] if e(sample) == 1
	scalar dmean_`outcome' = r(mean)
	qui sum eitcexp_fblinkseitcratepp [aw = pop2000] if e(sample) == 1
	scalar imean_`outcome' = r(mean)
}	

cd $output
cap file close regout
file open regout using tab_network_countyvar.tex, write replace
//Header
file write regout "\begin{tabular}{lccccc}" _n
file write regout "\toprule" _n
file write regout "\toprule" _n
file write regout "& \multicolumn{3}{c}{Percent of Returns with EITC} &  \\" _n
file write regout "\cmidrule{2-4}" _n
file write regout "& & & & Percent of EITC \\" _n
file write regout "&  Any EITC & No Self Employment & Self Employment & Returns with Self Emp. & Average EITC  \\" _n
file write regout "&  & (No Schedule C, E, or F) & (Schedule C, E, or F) &  (Schedule C, E, or F) & Amount \\" _n
file write regout "& (1) & (2) & (3) & (4) & (5) \\" _n
file write regout "\midrule" _n
file write regout " \\" _n
//Table
file write regout "Out-of-State Friends per person" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %7.2f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "Exposed to State EITC Scaled by" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
file write regout "County 1999 EITC Rate (Std.) & & & & & \\" _n
//Statistics
file write regout "Dependent Mean" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %5.1f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}  
file write regout "\\" _n
file write regout "\bottomrule" _n
file write regout "\bottomrule" _n
file write regout "\end{tabular}"
file close regout

/*******
Look at the other two measures as well
*******/
reghdfe eitc_rate eitcexp_fblinkseitcrangepp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
sum eitcexp_fblinkseitcrangepp [aw = pop2000] if  e(sample) == 1, de
gen std_eitcexp_fblinkseitcrangepp = (eitcexp_fblinkseitcrangepp-r(mean))/r(sd)
	
//Effect on Levels
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate {
	reghdfe `outcome' eitcexp_fblinkseitcrangepp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
}

reghdfe eitc_rate eitcexp_fblinksbunchratepp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
sum eitcexp_fblinksbunchratepp [aw = pop2000] if  e(sample) == 1, de
gen std_eitcexp_fblinksbunchratepp = (eitcexp_fblinksbunchratepp-r(mean))/r(sd)
	
//Effect on Levels
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	reghdfe `outcome' eitcexp_fblinksbunchratepp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
}
	

/******************************
Appendix Table A10. Impact of Social Network Exposure on EITC Claiming Behavior, Heterogeneity by Size of Eligible pool
******************************/
cd $data
use cty_eitcnetwork1999_2013, clear

gen eitcnonself_rate = ((eitc_returns-eitc_selfemp)/tot_returns)*100
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	gen ln`outcome' = ln(`outcome')
}
gen pct_eitcrange = ((tagi0+tagi5+tagi10+tagi15+tagi20+tagi25+tagi30+tagi35)/tot_returns)
gen bunch515 = ((eagi5+eagi10)/eitc_returns)
bys ctyfips: egen bunch515_2000 = max(bunch515*(year == 2000))

gen t = eitc_rate if year == 1999
bys ctyfips: egen eitc_rate1999 = max(t)
drop t
gen t = pct_eitcrange if year == 2000
bys ctyfips: egen pct_eitcrange2000 = max(t)

reghdfe eitc_rate eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1, de
gen std_eitcexp_fblinkspp = (eitcexp_fblinkspp-r(mean))/r(sd)
	
bys stfips: egen p50 = pctile(eitc_rate1999) if year == 1999, p(50)
bys stfips: egen eitcpct_p50 = max(p50)
gen hi = eitc_rate1999 >=eitcpct_p50
gen std_eitcexp_fblinkspp_hi = std_eitcexp_fblinkspp*hi

//Effect on Levels
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	reghdfe `outcome' std_eitcexp_fblinkspp std_eitcexp_fblinkspp_hi $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	qui sum `outcome' [aw = pop2000] if e(sample) == 1
	scalar dmean_`outcome' = r(mean)
	qui sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1
	scalar imean_`outcome' = r(mean)
}	

cd $output
cap file close regout
file open regout using tab_baseline_hi.tex, write replace
//Header
file write regout "\begin{tabular}{lccccc}" _n
file write regout "\toprule" _n
file write regout "\toprule" _n
file write regout "& \multicolumn{3}{c}{Percent of Returns with EITC} &  \\" _n
file write regout "\cmidrule{2-4}" _n
file write regout "& & & & Percent of EITC \\" _n
file write regout "&  Any EITC & No Self Employment & Self Employment & Returns with Self Emp. & Average EITC \\" _n
file write regout "&  & (No Schedule C, E, or F) & (Schedule C, E, or F) &  (Schedule C, E, or F) & Amount \\" _n
file write regout "& (1) & (2) & (3) & (4) & (5) \\" _n
file write regout "\midrule" _n
file write regout " \\" _n
//Table
file write regout "Out-of-State Friends per person" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %7.2f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "Exposed to State EITC (Standardized)" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
file write regout "Out-of-State Friends Exposed to EITC" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %7.2f (b_`outcome'[1,2])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "*1999 EITC Share Above State Median" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[2,2]) ) ")"
}
file write regout "\\" _n

file write regout "& & & & \\" _n
//Statistics
file write regout "Dependent Mean" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %5.1f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}  
file write regout "\\" _n
file write regout "\bottomrule" _n
file write regout "\bottomrule" _n
file write regout "\end{tabular}"
file close regout

/******************************
Appendix Table A11. Impact on Size of EITC and Earnings Distribution, Heterogeneity by Size of Eligible pool
******************************/
cd $data
use cty_eitcnetwork1999_2013, clear
foreach i in 0 5 10 15 20 25 30 35 40 {
	gen pcteitc_`i' = (eagi`i'/eitc_returns)*100
}

reghdfe eitc_rate eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster ctyfips)
sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1, de
gen std_eitcexp_fblinkspp = (eitcexp_fblinkspp-r(mean))/r(sd)
	

foreach outcome of varlist pcteitc* {
	gen ln`outcome' = ln(`outcome')
}
gen pct_eitcrange = ((tagi0+tagi5+tagi10+tagi15+tagi20+tagi25+tagi30+tagi35)/tot_returns)
gen bunch515 = ((eagi5+eagi10)/eitc_returns)
bys ctyfips: egen bunch515_2000 = max(bunch515*(year == 2000))

gen t = eitc_rate if year == 1999
bys ctyfips: egen eitc_rate1999 = max(t)
drop t
gen t = pct_eitcrange if year == 2000
bys ctyfips: egen pct_eitcrange2000 = max(t)

bys stfips: egen p50 = pctile(eitc_rate1999) if year == 1999, p(50)
bys stfips: egen eitcpct_p50 = max(p50)
gen hi = eitc_rate1999 >=eitcpct_p50
gen std_eitcexp_fblinkspp_hi = std_eitcexp_fblinkspp*hi

//Effect on Levels
foreach outcome in pcteitc_0 pcteitc_5 pcteitc_10 pcteitc_15 pcteitc_20 pcteitc_25 pcteitc_30 pcteitc_35 pcteitc_40 {
	reghdfe `outcome' std_eitcexp_fblinkspp std_eitcexp_fblinkspp_hi  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster ctyfips)
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	qui sum `outcome' [aw = pop2000] if e(sample) == 1
	scalar dmean_`outcome' = r(mean)
	qui sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1
	scalar imean_`outcome' = r(mean)
}	

cd $output
cap file close regout
file open regout using tab_earningsfbpp_hi.tex, write replace
//Header
file write regout "\begin{tabular}{lccccccccc}" _n
file write regout "\toprule" _n
file write regout "\toprule" _n
file write regout "& \multicolumn{9}{c}{Percent of EITC Returns by Income Bin}  \\" _n
file write regout "\cmidrule{2-10}" _n
file write regout `" & Under \\$5K & \\$5-10K & \\$10-15K & \\$15-20K & \\$20-25K & \\$25-30K & \\$30-35K & \\$35-40K & Over \\$40K \\"' _n
file write regout "& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) & (9) \\" _n
file write regout "\midrule" _n
file write regout " \\" _n
//Table
file write regout "Out-of-State Friends per person" 
foreach outcome in pcteitc_0 pcteitc_5 pcteitc_10 pcteitc_15 pcteitc_20 pcteitc_25 pcteitc_30 pcteitc_35 pcteitc_40 {
	file write regout "&" %7.2f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "Exposed to State EITC (Standardized)" 
foreach outcome in pcteitc_0 pcteitc_5 pcteitc_10 pcteitc_15 pcteitc_20 pcteitc_25 pcteitc_30 pcteitc_35 pcteitc_40 {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
file write regout "Out-of-State Friends Exposed to EITC" 
foreach outcome in pcteitc_0 pcteitc_5 pcteitc_10 pcteitc_15 pcteitc_20 pcteitc_25 pcteitc_30 pcteitc_35 pcteitc_40 {
	file write regout "&" %7.2f (b_`outcome'[1,2])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "*1999 EITC Share Above State Median" 
foreach outcome in pcteitc_0 pcteitc_5 pcteitc_10 pcteitc_15 pcteitc_20 pcteitc_25 pcteitc_30 pcteitc_35 pcteitc_40 {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[2,2]) ) ")"
}
file write regout "\\" _n

file write regout " \\" _n
//Statistics
file write regout "Dependent Mean" 
foreach outcome in pcteitc_0 pcteitc_5 pcteitc_10 pcteitc_15 pcteitc_20 pcteitc_25 pcteitc_30 pcteitc_35 pcteitc_40 {
	file write regout "&" %5.1f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in pcteitc_0 pcteitc_5 pcteitc_10 pcteitc_15 pcteitc_20 pcteitc_25 pcteitc_30 pcteitc_35 pcteitc_40 {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}  
file write regout "\\" _n
file write regout "\bottomrule" _n
file write regout "\bottomrule" _n
file write regout "\end{tabular}"
file close regout

/******************************
Appendix Table A12. Impact of Social Network Exposure on EITC Claiming Behavior, Exploiting Intensive Margin Variation in State EITC rates
******************************/
cd $data
use cty_eitcnetwork1999_2013, clear

gen eitcnonself_rate = ((eitc_returns-eitc_selfemp)/tot_returns)*100
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	gen ln`outcome' = ln(`outcome')
}
reghdfe eitc_rate steitcexp_fblinkpctpp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
sum steitcexp_fblinkpctpp [aw = pop2000] if  e(sample) == 1, de
gen std_steitcexp_fblinkpctpp = (steitcexp_fblinkpctpp-r(mean))/r(sd)
	
//Effect on Levels
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	reghdfe `outcome' std_steitcexp_fblinkpctpp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	qui sum `outcome' [aw = pop2000] if e(sample) == 1
	scalar dmean_`outcome' = r(mean)
	qui sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1
	scalar imean_`outcome' = r(mean)
}	

cd $output
cap file close regout
file open regout using tab_intensivemargineitcrate.tex, write replace
//Header
file write regout "\begin{tabular}{lccccc}" _n
file write regout "\toprule" _n
file write regout "\toprule" _n
file write regout "& \multicolumn{3}{c}{Percent of Returns with EITC} &  \\" _n
file write regout "\cmidrule{2-4}" _n
file write regout "& & & & Percent of EITC \\" _n
file write regout "&  Any EITC & No Self Employment & Self Employment & Returns with Self Emp. & Average EITC  \\" _n
file write regout "&  & (No Schedule C, E, or F) & (Schedule C, E, or F) &  (Schedule C, E, or F) & Amount \\" _n
file write regout "& (1) & (2) & (3) & (4) & (5) \\" _n
file write regout "\midrule" _n
file write regout "\\" _n
//Table
file write regout "Share Out-of-State Friends per person" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %7.2f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "*Friends' State EITC (Standardized)" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
file write regout "& & & & \\" _n
//Statistics
file write regout "Dependent Mean" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %5.1f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}  
file write regout "\\" _n
file write regout "\bottomrule" _n
file write regout "\bottomrule" _n
file write regout "\end{tabular}"
file close regout

/******************************
Table A13. Mediating Role of High Speed Internet
******************************/
cd $data
use cty_eitcnetwork1999_2013, clear

merge 1:1 ctyfips year using provider_levelcty
drop if _m == 2
drop _m


gen eitcnonself_rate = ((eitc_returns-eitc_selfemp)/tot_returns)*100
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	gen ln`outcome' = ln(`outcome')
}
reghdfe eitc_rate eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
sum eitcexp_fblinkspp [aw = pop2000] if  e(sample) == 1, de
gen std_eitcexp_fblinkspp = (eitcexp_fblinkspp-r(mean))/r(sd)
	

gen std_eitcexp_fblinkspp_hs = std_eitcexp_fblinkspp*num_prov
gen std_eitcexp_fblinkspp_prhs = std_eitcexp_fblinkspp*prednum_prov

foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	reghdfe `outcome' std_eitcexp_fblinkspp_hs std_eitcexp_fblinkspp num_prov  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	qui sum `outcome' [aw = pop2000] if e(sample) == 1
	scalar dmean_`outcome' = r(mean)
	qui sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1
	scalar imean_`outcome' = r(mean)
}	
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc  {
	reghdfe `outcome' std_eitcexp_fblinkspp_prhs std_eitcexp_fblinkspp prednum_prov  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
	matrix b_2`outcome' = e(b)
	matrix V_2`outcome' = e(V)
	scalar edf_r2`outcome' = e(df_r)
	scalar n_2`outcome' = e(N)
	qui sum `outcome' [aw = pop2000] if e(sample) == 1
	scalar dmean_2`outcome' = r(mean)
	qui sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1
	scalar imean_2`outcome' = r(mean)
}

cd $output
cap file close regout
file open regout using tab_highspeed.tex, write replace
//Header
file write regout "\begin{tabular}{lccccc}" _n
file write regout "\toprule" _n
file write regout "\toprule" _n
file write regout "& \multicolumn{3}{c}{Percent of Returns with EITC} &  \\" _n
file write regout "\cmidrule{2-4}" _n
file write regout "& & & & Percent of EITC \\" _n
file write regout "&  Any EITC & No Self Employment & Self Employment & Returns with Self Emp. & Average EITC  \\" _n
file write regout "&  & (No Schedule C, E, or F) & (Schedule C, E, or F) &  (Schedule C, E, or F) & Amount \\" _n
file write regout "& (1) & (2) & (3) & (4) & (5) \\" _n
file write regout "\midrule" _n
file write regout "Panel A. & \multicolumn{5}{c}{High Speed Provider Data from 200-2007} \\" _n
//Table
file write regout "Out-of-State Friends per person Exposed to " 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %7.2f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "State EITC*Num. High Speed Providers" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
file write regout "Out-of-State Friends per person" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %7.2f (b_`outcome'[1,2])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "Exposed to State EITC (Standardized)" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[2,2]) ) ")"
}
file write regout "\\" _n
file write regout "Num. High Speed Providers" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %7.2f (b_`outcome'[1,3])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[3,3]) ) ")"
}
file write regout "\\" _n
file write regout "& & & & \\" _n
//Statistics
file write regout "Dependent Mean" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc{
	file write regout "&" %5.1f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc{
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}  
file write regout "\\" _n
file write regout "\midrule" _n
file write regout "Panel B. & \multicolumn{5}{c}{High Speed Provider Data from 2000-2007, Predicted from 2008-2013} \\" _n

file write regout "Out-of-State Friends per person Exposed to " 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %7.2f (b_2`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r2`outcome')',abs(b_2`outcome'[1,1]/(sqrt(V_2`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r2`outcome')',abs(b_2`outcome'[1,1]/(sqrt(V_2`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r2`outcome')',abs(b_2`outcome'[1,1]/(sqrt(V_2`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r2`outcome')',abs(b_2`outcome'[1,1]/(sqrt(V_2`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r2`outcome')',abs(b_2`outcome'[1,1]/(sqrt(V_2`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "State EITC*Num. High Speed Providers" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" "(" %5.2f ( sqrt(V_2`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
file write regout "Out-of-State Friends per person" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %7.2f (b_`outcome'[1,2])
	if 2*(ttail(`=scalar(edf_r2`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r2`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) >=.01 & 2*(ttail(`=scalar(edf_r2`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r2`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) >=.05 & 2*(ttail(`=scalar(edf_r2`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "Exposed to State EITC (Standardized)" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[2,2]) ) ")"
}
file write regout "\\" _n
file write regout "Num. High Speed Providers" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %7.2f (b_`outcome'[1,3])
	if 2*(ttail(`=scalar(edf_r2`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r2`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) >=.01 & 2*(ttail(`=scalar(edf_r2`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r2`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) >=.05 & 2*(ttail(`=scalar(edf_r2`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[3,3]) ) ")"
}
file write regout "\\" _n
file write regout "& & & & \\" _n
//Statistics
file write regout "Dependent Mean" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %5.1f (`=scalar(dmean_2`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %12.0fc (`=scalar(n_2`outcome')') 
}  
file write regout "\\" _n
file write regout "\bottomrule" _n
file write regout "\bottomrule" _n
file write regout "\end{tabular}"
file close regout


/******************************
Appendix Table A14. Differences in Effects Before and After Facebook Started
******************************/
cd $data
use cty_eitcnetwork1999_2013, clear

gen eitcnonself_rate = ((eitc_returns-eitc_selfemp)/tot_returns)*100
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	gen ln`outcome' = ln(`outcome')
}

reghdfe eitc_rate eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
	qui sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1, de
gen std_eitcexp_fblinkspp = (eitcexp_fblinkspp-r(mean))/r(sd)

foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	reghdfe `outcome' std_eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000 & year<2004, absorb(ctyfips st_yr) vce(cluster stfips)
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	qui sum `outcome' [aw = pop2000] if e(sample) == 1
	scalar dmean_`outcome' = r(mean)
	qui sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1
	scalar imean_`outcome' = r(mean)
}	
	
cd $output
cap file close regout
file open regout using tab_effectprepost_fb.tex, write replace
//Header
file write regout "\begin{tabular}{lccccc}" _n
file write regout "\toprule" _n
file write regout "\toprule" _n
file write regout "& \multicolumn{3}{c}{Percent of Returns with EITC} &  \\" _n
file write regout "\cmidrule{2-4}" _n
file write regout "& & & & Percent of EITC \\" _n
file write regout "&  Any EITC & No Self Employment & Self Employment & Returns with Self Emp. & Average EITC \\" _n
file write regout "&  & (No Schedule C, E, or F) & (Schedule C, E, or F) &  (Schedule C, E, or F) & Amount \\" _n
file write regout "& (1) & (2) & (3) & (4) & (5) \\" _n
file write regout "\midrule" _n
file write regout "& \multicolumn{5}{c}{Before Facebook Began (Pre 2004)} \\" _n
file write regout "\\" _n
//Table
file write regout "Out-of-State Friends per person" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %7.2f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "Exposed to State EITC (Standardized)" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
file write regout " \\" _n
//Statistics
file write regout "Dependent Mean" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %5.1f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}  
file write regout "\\" _n

foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	reghdfe `outcome' std_eitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & year>=2004, absorb(ctyfips st_yr) vce(cluster stfips)
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	qui sum `outcome' [aw = pop2000] if e(sample) == 1
	scalar dmean_`outcome' = r(mean)
	qui sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1
	scalar imean_`outcome' = r(mean)
}	

file write regout "\midrule" _n
file write regout "& \multicolumn{5}{c}{After Facebook Began (Post 2004)} \\" _n
file write regout " \\" _n
//Table
file write regout "Out-of-State Friends per person" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %7.2f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "Exposed to State EITC (Standardized)" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
file write regout " \\" _n
//Statistics
file write regout "Dependent Mean" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %5.1f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}  
file write regout "\\" _n
file write regout "\bottomrule" _n
file write regout "\bottomrule" _n
file write regout "\end{tabular}"
file close regout

/****************************
Appendix Table A15. "First Stage" Estimates of the Distance*Interstate Prediction
****************************/
//First Create a table with the "First Stage" Predictions
cd $data
use sci_distpred, clear

gen double fb_share = fb_link/tot_ostatef
replace dist = dist/1000
gen dist_same_interstate = dist*same_interstate

local models = `" "dist" "same_interstate" "dist same_interstate dist_same_interstate" "'
forval outcome = 1/3 {
	local model: word `outcome' of `models'
	reghdfe fb_links `model' if stfips ~= f_stfips, absorb(ctyfips, savefe) vce(cluster stfips) 
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	qui sum fb_links if e(sample) == 1
	scalar dmean_`outcome' = r(mean)
	qui sum dist if e(sample) == 1
	scalar imean_`outcome' = r(mean)
	qui sum same_interstate if e(sample) == 1
	scalar imean2_`outcome' = r(mean)
	testparm *
	scalar r_f`outcome' = r(F)
}	

cd $output
cap file close regout
file open regout using tab_ivfirststage.tex, write replace
//Header
file write regout "\begin{tabular}{lccc}" _n
file write regout "\toprule" _n
file write regout "\toprule" _n
file write regout "& \multicolumn{3}{c}{Scaled Number of Friends}  \\" _n
file write regout "& (1) & (2) & (3) \\" _n
file write regout "\midrule" _n
file write regout "& & & \\" _n
//Table
file write regout "Distance (1000s miles)" 
file write regout "&" %7.1f (b_1[1,1])
		if 2*(ttail(`=scalar(edf_r1)',abs(b_1[1,1]/(sqrt(V_1[1,1]))))) <.01 {
			file write regout "***"
		}
		if 2*(ttail(`=scalar(edf_r1)',abs(b_1[1,1]/(sqrt(V_1[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r1)',abs(b_1[1,1]/(sqrt(V_1[1,1]))))) <.05{
			file write regout "**"
		}
		if 2*(ttail(`=scalar(edf_r1)',abs(b_1[1,1]/(sqrt(V_1[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r1)',abs(b_1[1,1]/(sqrt(V_1[1,1]))))) <.10{
			file write regout "*"
		}
file write regout "& &" %7.1f (b_3[1,1])
		if 2*(ttail(`=scalar(edf_r3)',abs(b_3[1,1]/(sqrt(V_3[1,1]))))) <.01 {
			file write regout "***"
		}
		if 2*(ttail(`=scalar(edf_r3)',abs(b_3[1,1]/(sqrt(V_3[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r3)',abs(b_3[1,1]/(sqrt(V_3[1,1]))))) <.05{
			file write regout "**"
		}
		if 2*(ttail(`=scalar(edf_r3)',abs(b_3[1,1]/(sqrt(V_3[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r3)',abs(b_3[1,1]/(sqrt(V_3[1,1]))))) <.10{
			file write regout "*"
		}
file write regout " \\" _n
file write regout "&" "(" %5.1f ( sqrt(V_1[1,1]) ) ")" "& &" "(" %5.1f ( sqrt(V_3[1,1]) ) ")" "\\" _n

file write regout "Same Interstate" 
file write regout "& &" %7.1f (b_2[1,1])
		if 2*(ttail(`=scalar(edf_r2)',abs(b_2[1,1]/(sqrt(V_2[1,1]))))) <.01 {
			file write regout "***"
		}
		if 2*(ttail(`=scalar(edf_r2)',abs(b_2[1,1]/(sqrt(V_2[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r2)',abs(b_2[1,1]/(sqrt(V_2[1,1]))))) <.05{
			file write regout "**"
		}
		if 2*(ttail(`=scalar(edf_r2)',abs(b_2[1,1]/(sqrt(V_2[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r2)',abs(b_2[1,1]/(sqrt(V_2[1,1]))))) <.10{
			file write regout "*"
		}
file write regout "& " %7.1f (b_3[1,2])
		if 2*(ttail(`=scalar(edf_r3)',abs(b_3[1,2]/(sqrt(V_3[2,2]))))) <.01 {
			file write regout "***"
		}
		if 2*(ttail(`=scalar(edf_r3)',abs(b_3[1,2]/(sqrt(V_3[2,2]))))) >=.01 & 2*(ttail(`=scalar(edf_r3)',abs(b_3[1,2]/(sqrt(V_3[2,2]))))) <.05{
			file write regout "**"
		}
		if 2*(ttail(`=scalar(edf_r3)',abs(b_3[1,2]/(sqrt(V_3[2,2]))))) >=.05 & 2*(ttail(`=scalar(edf_r3)',abs(b_3[1,2]/(sqrt(V_3[2,2]))))) <.10{
			file write regout "*"
		}
file write regout " \\" _n
file write regout "& & " "(" %5.1f ( sqrt(V_2[1,1]) ) ")" "&" "(" %5.1f ( sqrt(V_3[2,2]) ) ")" "\\" _n

file write regout "Distance"
file write regout "& & & " %7.1f (b_3[1,3])
		if 2*(ttail(`=scalar(edf_r3)',abs(b_3[1,3]/(sqrt(V_3[3,3]))))) <.01 {
			file write regout "***"
		}
		if 2*(ttail(`=scalar(edf_r3)',abs(b_3[1,3]/(sqrt(V_3[3,3]))))) >=.01 & 2*(ttail(`=scalar(edf_r3)',abs(b_3[1,3]/(sqrt(V_3[3,3]))))) <.05{
			file write regout "**"
		}
		if 2*(ttail(`=scalar(edf_r3)',abs(b_3[1,3]/(sqrt(V_3[3,3]))))) >=.05 & 2*(ttail(`=scalar(edf_r3)',abs(b_3[1,3]/(sqrt(V_3[3,3]))))) <.10{
			file write regout "*"
		}
file write regout " \\" _n
file write regout "\hspace{5mm}*Same Interstate & & & "  "(" %5.1f ( sqrt(V_3[3,3]) ) ")" "\\" _n


file write regout "& & & \\" _n
//Statistics
file write regout "F-statistic"
forval outcome = 1/3 {
	file write regout "&" %5.1f (`=scalar(r_f`outcome')')
}
file write regout "\\" _n
file write regout "& & & \\" _n
file write regout "Dependent Mean" 
forval outcome = 1/3 {
	file write regout "&" %5.1f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
forval outcome = 1/3  {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}  
file write regout "\\" _n
file write regout "\bottomrule" _n
file write regout "\bottomrule" _n
file write regout "\end{tabular}"
file close regout


/******************************
Appendix Table A16. Impact of Social Network Exposure on EITC Claiming Behavior
"IV" Predicted Measure
******************************/
cd $data
import excel using NCHSURCodes2013.xlsx, clear first
rename code urbanscale2013
rename H urbanscale2006
rename basedcode urbanscale1990
destring urbanscale1990, replace

rename FIPScode ctyfips
keep ctyfips urbanscale*

merge 1:m ctyfips using cty_eitcnetwork1999_2013
drop if _m == 1
drop _m

gen eitcnonself_rate = ((eitc_returns-eitc_selfemp)/tot_returns)*100
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	gen ln`outcome' = ln(`outcome')
}
gen lneitcexp_fblinkspp = ln(eitcexp_fblinkspp)
gen lnsteitc_pct = ln(steitc_pct)

reghdfe eitc_rate predeitcexp_fblinkspp  $controls [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
sum predeitcexp_fblinkspp [aw = pop2000] if e(sample) == 1, de
gen std_predeitcexp_fblinkspp = (predeitcexp_fblinkspp-r(mean))/r(sd)

sum eitcexp_fblinkspp [aw = pop2000] if e(sample) == 1, de
gen std_eitcexp_fblinkspp = (eitcexp_fblinkspp-r(mean))/r(sd)


//Effect on Levels
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	reghdfe `outcome' $controls (std_eitcexp_fblinkspp = std_predeitcexp_fblinkspp) [aw = pop2000] if pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
	matrix b_`outcome' = e(b)
	matrix V_`outcome' = e(V)
	scalar edf_r`outcome' = e(df_r)
	scalar n_`outcome' = e(N)
	qui sum `outcome' [aw = pop2000] if e(sample) == 1
	scalar dmean_`outcome' = r(mean)
	qui sum steitc_pct [aw = pop2000] if e(sample) == 1
	scalar imean_`outcome' = r(mean)
}	
//Exclude the Urban, Metro Center counties
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	reghdfe `outcome' $controls (std_eitcexp_fblinkspp = std_predeitcexp_fblinkspp) [aw = pop2000] if urbanscale2006>=2 & pop2000 ~= . & year>=2000, absorb(ctyfips st_yr) vce(cluster stfips)
	matrix b_2`outcome' = e(b)
	matrix V_2`outcome' = e(V)
	scalar edf_r2`outcome' = e(df_r)
	scalar n_2`outcome' = e(N)
	qui sum `outcome' [aw = pop2000] if e(sample) == 1
	scalar dmean_2`outcome' = r(mean)
	qui sum steitc_pct [aw = pop2000] if e(sample) == 1
	scalar imean_2`outcome' = r(mean)
}	

cd $output
cap file close regout
file open regout using tab_ivfblinkspp_2sls.tex, write replace
//Header
file write regout "\begin{tabular}{lccccc}" _n
file write regout "\toprule" _n
file write regout "\toprule" _n
file write regout "& \multicolumn{3}{c}{Percent of Returns with EITC} &  \\" _n
file write regout "\cmidrule{2-4}" _n
file write regout "& & & & Percent of EITC \\" _n
file write regout "&  Any EITC & No Self Employment & Self Employment & Returns with Self Emp. & Average EITC  \\" _n
file write regout "&  & (No Schedule C, E, or F) & (Schedule C, E, or F) &  (Schedule C, E, or F) & Amount \\" _n
file write regout "& (1) & (2) & (3) & (4) \\" _n
file write regout "\midrule" _n
file write regout "\\" _n
//Table
file write regout "Out-of-State Friends per person" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %7.2f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "Exposed to State EITC (Standardized)" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
file write regout "& & & & \\" _n
//Statistics
file write regout "Dependent Mean" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %5.1f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in eitc_rate eitcnonself_rate eitcself_rate selfamongeitc_rate ave_eitc {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}  
file write regout "\\" _n
file write regout "\midrule" _n
file write regout "& \multicolumn{4}{c}{Exclude Central Counties of MSAs} \\" _n
//Table
file write regout "Out-of-State Friends per person" 
foreach outcome in 2eitc_rate 2eitcnonself_rate 2eitcself_rate 2selfamongeitc_rate 2ave_eitc {
	file write regout "&" %7.2f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
file write regout "Exposed to State EITC (Standardized)" 
foreach outcome in 2eitc_rate 2eitcnonself_rate 2eitcself_rate 2selfamongeitc_rate 2ave_eitc {
	file write regout "&" "(" %5.2f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
file write regout "& & & & \\" _n
//Statistics
file write regout "Dependent Mean" 
foreach outcome in 2eitc_rate 2eitcnonself_rate 2eitcself_rate 2selfamongeitc_rate 2ave_eitc {
	file write regout "&" %5.1f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in 2eitc_rate 2eitcnonself_rate 2eitcself_rate 2selfamongeitc_rate 2ave_eitc {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}  
file write regout "\\" _n
file write regout "\bottomrule" _n
file write regout "\bottomrule" _n
file write regout "\end{tabular}"
file close regout

/*****************************
//////////////////////////////
APPENDIX B. FIGURES DESCRIPTIVE ANALYSIS OF TWITTER
//////////////////////////////
*****************************/
/****************************
Appendix Figure B1. Tweet, Re-tweet, and Lik Frequencies over time
****************************/
//Plot the number of tweets 
cd $data
use eitc_tweets2010_2019, clear
gen edate = mdy(month,day,year)
gen week = week(edate)
drop if football_club == 1
gen one = 1

/*I am going to exclude three sets of tweets 
(1)by Ivanka Trump right around tax season in 2019. It had 4170 retweets and 17853 likes
(2) by Representative Joe Kennedy. It had 3554 retweets and 9621 likes
(3) a series of 3 tweets by Noah Smith. They had 1728+502+407 retweets and 3487+1905+1663 likes
*/
drop if year == 2019 & month == 4 & id == 13884 & handle == "IvankaTrump"
drop if year == 2017 & month == 11 & id == 21660 & handle == "RepJoeKennedy"
drop if year == 2017 & month == 6 & handle == "Noahpinion" & inlist(id,13881,13872,13871)
collapse (sum) one retweets likes, by(week year)

gen yr_wk = year+((week-1)/52)
sort yr_wk
cd $output
twoway (line one yr_wk) ///
	, graphregion(color(white)) ytitle(Weekly Number of EITC Tweets) xtitle(Year) ///
	xlabel(2010(1)2019)
	
graph export weekly_tweets.png, replace
	
twoway (line retweets yr_wk) ///
	(line likes yr_wk) ///
	, legend(order(1 "Re-Tweets" 2 "Likes") rows(1)) ///
	graphregion(color(white)) xlabel(2010(1)2019) ytitle(Weekly Number of EITC Tweets) xtitle(Year)

graph export weekly_retweetslikes.png, replace

//Note the Tweets I have excluded

//Plot the number of tweets 
cd $data
use eitc_tweets2010_2019, clear
gen edate = mdy(month,day,year)
gen week = week(edate)
drop if football_club == 1
gen one = 1
gen outlier = 0
/*I am going to exclude three sets of tweets 
(1)by Ivanka Trump right around tax season in 2019. It had 4170 retweets and 17853 likes
(2) by Representative Joe Kennedy. It had 3554 retweets and 9621 likes
(3) a series of 3 tweets by Noah Smith. They had 1728+502+407 retweets and 3487+1905+1663 likes
*/

replace outlier = 1 if year == 2019 & month == 4 & id == 13884 & handle == "IvankaTrump"
replace outlier = 1 if year == 2017 & month == 11 & id == 21660 & handle == "RepJoeKennedy"
replace outlier = 1 if year == 2017 & month == 6 & handle == "Noahpinion" & inlist(id,13881,13872,13871)

//I want it to be the same over time so I can't include part year 2019
keep if year<=2018
bys year: egen tottweets = sum(one)
gen shr_twt = one/tottweets
bys year: egen totretweet = sum(retweets)
gen shr_retwt = retweets/totretweet
bys year: egen totlikes = sum(likes)
gen shr_likes = likes/totlikes

gen one_o = 1 if outlier == 0
gen likes_o = likes if outlier == 0
gen retweets_o = retweets if outlier == 0
collapse (sum) one retweets likes one_o likes_o retweets_o shr_twt shr_retwt shr_likes, by(week year)

collapse (sum) one retweets likes one_o likes_o retweets_o (mean) shr_twt shr_retwt shr_likes, by(week)

cd $output

local x_line = week(mdy(4,15,2010))
twoway (line one_o week, lcolor(navy) lpattern(--)) ///
	(line one week, lcolor(navy) xline(`x_line', lcolor(black) lpattern(__))) ///
	, graphregion(color(white)) ytitle(Weekly Number of EITC Tweets) xtitle(Week of Year) ///
	xlabel(0(5)50) legend(off)
	
graph export tweets_byweek.png, replace

local x_line = week(mdy(4,15,2010))
twoway (line likes_o week, lcolor(navy) lpattern(--)) ///
	(line likes week, lcolor(navy) xline(`x_line', lcolor(black) lpattern(__)) ///
	text(12000 27.9  "Noah Smith", size(vsmall)) ///
	text(11500 27.9  "2017 tweets", size(vsmall)) ///
	text(14000 49.5  "Rep. Joe Kennedy", size(vsmall)) ///
	text(13500 49.5  "2017 tweet", size(vsmall))) ///
	, graphregion(color(white)) ytitle(Weekly Number of EITC Tweet Likes) xtitle(Week of Year) ///
	xlabel(0(5)50) legend(off)
graph export likes_byweek.png, replace

local x_line = week(mdy(4,15,2010))
twoway (line retweets_o week, lcolor(navy) lpattern(--)) ///
	(line retweets week, lcolor(navy) xline(`x_line', lcolor(black) lpattern(__)) ///
	text(4500 27.9  "Noah Smith", size(vsmall)) ///
	text(4240 27.9  "2017 tweets", size(vsmall)) ///
	text(5700 49.5  "Rep. Joe Kennedy", size(vsmall)) ///
	text(5400 49.5  "2017 tweet", size(vsmall))) ///
	, graphregion(color(white)) ytitle(Weekly Number of EITC Re-Tweets) xtitle(Week of Year) ///
	xlabel(0(5)50) legend(off)
graph export retweets_byweek.png, replace

/******************************
Figure B2 and B3 Words in Tweets about the EITC
******************************/
cd $data
use tweet_words, clear
replace t_ = "_picture" if regexm(t_,"pic\.twitter") == 1
replace t_ = "_mention" if regexm(t_,"@") == 1
replace t_ = regexr(t_,"\!+$","")
replace t_ = regexr(t_,"\.+$","")
replace t_ = regexr(t_,"\:+$","")
replace t_ = regexr(t_,"\,+$","")
replace t_ = regexr(t_,"\?+$","")
replace t_ = lower(t_)
drop n
//Now I only want to count each tweet once if there are two of the same word
bys id year month t_ : gen n = _n
drop if n >1
collapse (sum) one retweets likes, by(t_)
compress
gen filler = 0
//100 most common Englis words plus an (variation of a) and is and are (to be)
foreach w in a an about all also and as at be because but by can come could day do even ///
	find first for from get give go have he her here him his how i if in into it ///
	its just know like look make man many me more my new no not now of on one only ///
	or other our out people say see she so some take tell than  that the their them ///
	then there these they thing think this those time to two up use very want way ///
	we well what when which who will with would year you your & is are {
	
	replace filler = 1 if t_ == "`w'"
}
gen punct = 0 
foreach p in "..." "-" "" "|" "…" {
	replace punct = 1 if t_ == "`p'"
}

twoway (scatter retweets one if filler == 0 & punct == 0)
replace t_ = "@mention" if t_ == "_mention"
replace t_ = "w/picture" if t_ == "_picture"
replace t_ = "w/link" if t_ == "http"

cd $output
twoway (scatter retweets one if filler == 0 & punct == 0 & retweets>=9000, mlabel(t_) mlabposition(0) mstyle(none)) ///
	(scatter retweets one if filler == 0 & punct == 0 & retweets<9000, mcolor(navy)) ///
	, legend(off) graphregion(color(white)) xtitle(Number of Tweets with _____) ytitle(Times Containing Tweet Re-Tweeted) 

graph export tweets_wordretweet.png, replace

twoway (scatter likes one if filler == 0 & punct == 0 & likes>=10000, mlabel(t_) mlabposition(0) mstyle(none)) ///
	(scatter likes one if filler == 0 & punct == 0 & likes<10000, mcolor(navy)) ///
	, legend(off) graphregion(color(white)) xtitle(Number of Tweets with _____) ytitle(Times Containing Tweet Liked)
	
graph export tweets_wordlikes.png, replace

//look at lower levels in more detail (Appendix Figures)

twoway (scatter retweets one if filler == 0 & punct == 0 & one>5 & one<15000 , mlabel(t_) mlabposition(0) mstyle(none)) ///
	, legend(off) graphregion(color(white)) xtitle(Number of Tweets with _____) ytitle(Times Containing Tweet Re-Tweeted)
graph export tweets_wordretweet15.png, replace
twoway (scatter retweets one if filler == 0 & punct == 0 & one>5 & one<5000 , mlabel(t_) mlabposition(0) mstyle(none)) ///
	, legend(off) graphregion(color(white)) xtitle(Number of Tweets with _____) ytitle(Times Containing Tweet Re-Tweeted)
graph export tweets_wordretweet5.png, replace
twoway (scatter retweets one if filler == 0 & punct == 0 & one>5 & one<2000 , mlabel(t_) mlabposition(0) mstyle(none)) ///
	, legend(off) graphregion(color(white)) xtitle(Number of Tweets with _____) ytitle(Times Containing Tweet Re-Tweeted)
graph export tweets_wordretweet2.png, replace
twoway (scatter retweets one if filler == 0 & punct == 0 & one>5 & one<1000 , mlabel(t_) mlabposition(0) mstyle(none)) ///
	, legend(off) graphregion(color(white)) xtitle(Number of Tweets with _____) ytitle(Times Containing Tweet Re-Tweeted)
graph export tweets_wordretweet1.png, replace

twoway (scatter likes one if filler == 0 & punct == 0 & one>5 & one<15000 , mlabel(t_) mlabposition(0) mstyle(none)) ///
	, legend(off) graphregion(color(white)) xtitle(Number of Tweets with _____) ytitle(Times Containing Tweet Liked)
graph export tweets_wordlikes15.png, replace	
twoway (scatter likes one if filler == 0 & punct == 0 & one>5 & one<5000 , mlabel(t_) mlabposition(0) mstyle(none)) ///
	, legend(off) graphregion(color(white)) xtitle(Number of Tweets with _____) ytitle(Times Containing Tweet Liked)
graph export tweets_wordlikes5.png, replace
twoway (scatter likes one if filler == 0 & punct == 0 & one>5 & one<2000 , mlabel(t_) mlabposition(0) mstyle(none)) ///
	, legend(off) graphregion(color(white)) xtitle(Number of Tweets with _____) ytitle(Times Containing Tweet Liked)
graph export tweets_wordlikes2.png, replace
twoway (scatter likes one if filler == 0 & punct == 0 & one>5 & one<1000 , mlabel(t_) mlabposition(0) mstyle(none)) ///
	, legend(off) graphregion(color(white)) xtitle(Number of Tweets with _____) ytitle(Times Containing Tweet Liked)
graph export tweets_wordlikes1.png, replace

